what wrong whit nested class constexpr ?

This code fails to compile with gcc 4.7.1

1
2
3
4
struct Test{
     struct Sub{constexpr Sub(int i){}};
    static constexpr Sub s=0;
};


"error: 'constexpr Test::Sub::Sub(int)' called in a constant expression"

While this code compiles successfully

1
2
3
4
struct Sub{constexpr Sub(int i){}};
struct Test{
    static constexpr Sub s=0;
};


I don't quite get it, what's wrong ?
Great question, Stackoverflow community would love it.

I may be wrong, but I think that compilation fails because at line 3, the class Test is not yet complete, which, in turn must mean the class Test::Sub is not complete in some sense (although I am not sure about it), which in turn means that the constructor Test::Sub::Sub(int) is declared, but not defined.

The last part is based on what clang++ tells me:
 undefined constructor 'Sub' cannot be used in a constant
      expression
test.cc:12:14: note: declared here
       struct Sub{constexpr Sub(int i){}};
                             ^


This is probably a defect, which is probably covered by the various fixes from this post-C++11 proposal:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3308.pdf
Last edited on
Tks.
I've been trying to use constexpr to build compile-time structures and I got quite confused with that error. It cost me hours to come up with that simple statement to show where the error came from...
:(

I went through the post-c++ 11 proposal. Unfortunately, I couldn't find any information about that error ...
Last edited on
there is a simillar discussion on stack overflow community,

http://stackoverflow.com/questions/8108314/error-using-a-constexpr-as-a-template-parameter-within-the-same-class/13775154#13775154

the key is "inline function definitions are treated as though they were defined just after the class definition" ...

Last edited on
Topic archived. No new replies allowed.