'constexpr' non-static member function will not be implicitly 'const' in C++1y; add 'const' to avoid a change in behavior

StackOverflow https://stackoverflow.com/questions/23124463

Question

clang++ give the following warning(see code below):

'constexpr' non-static member function will not be implicitly 'const' in C++1y; add 'const' to avoid a change in behavior

Where should the const be add so? const constexpr size_t getSize() { give another warning:

'const' type qualifier on return type has no effect

code:

constexpr size_t getSize()
{
    return sizeof(header);
}
Was it helpful?

Solution

I believe it's telling you that the member function can't be called on a const object as of C++1y.

Add const after getSize() to make it a const member function:

constexpr size_t getsize() const { ... }

OTHER TIPS

Complete testcase:

struct S {
  constexpr int getSize();
};

Diagnostic:

tmp.cc:2:17: warning: 'constexpr' non-static member function will not be
        implicitly 'const' in C++1y; add 'const' to avoid a change in
        behavior [-Wconstexpr-not-const]
  constexpr int getSize();
                ^
                          const

Note in particular the bottom line of the output. That's called a 'fix-it hint' and shows you the text you need to insert (and where to insert it) to fix the problem.

(In this case, the text starts with a leading space, making it slightly less clear that it should be inserted before the semicolon, not afterwards.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top