Hello,
I do some experiment with constexpr, I saw some error:
Minimal code, for reproduction:
#include <iostream>
template < typename Type >
struct A
{
static constexpr Type Func0() { return static_cast< Type >( 0.f ); }
static constexpr Type Func1() { return static_cast< Type >( 1.f ); }
};
template < typename Type >
struct Object
{
constexpr Object( Type const _a, Type const _b ) :
a( _a ),
b( _b )
{}
private:
Type a;
Type b;
};
template < typename Type >
struct Const
{
static constexpr Type Func0Value = A< Type >::Func0();
static constexpr Type Func1Value = A< Type >::Func1();
static constexpr Object< Type > myObject( A< Type >::Func0(), A< Type >::Func1() );
};
int main()
{
std::cout << Const< int >::Func0Value << std::endl;
std::cout << Const< int >::Func1Value << std::endl;
std::cout << Const< float >::Func1Value << std::endl;
return 0;
}
Error:
1>main.cpp(28): error : function "A<Type>::Func0 [with Type=int]" is not a type name
1> static constexpr Object< Type > myObject( A< Type >::Func0(), A< Type >::Func1() );
1> ^
1> detected during instantiation of class "Const<Type> [with Type=int]" at line 33
1>
1>main.cpp(28): error : function "A<Type>::Func1 [with Type=int]" is not a type name
1> static constexpr Object< Type > myObject( A< Type >::Func0(), A< Type >::Func1() );
1> ^
1> detected during instantiation of class "Const<Type> [with Type=int]" at line 33
1>
1>main.cpp(28): error : function "A<Type>::Func0 [with Type=float]" is not a type name
1> static constexpr Object< Type > myObject( A< Type >::Func0(), A< Type >::Func1() );
1> ^
1> detected during instantiation of class "Const<Type> [with Type=float]" at line 35
1>
1>main.cpp(28): error : function "A<Type>::Func1 [with Type=float]" is not a type name
1> static constexpr Object< Type > myObject( A< Type >::Func0(), A< Type >::Func1() );
1> ^
1> detected during instantiation of class "Const<Type> [with Type=float]" at line 35
1>
1> compilation aborted for main.cpp (code 2)
Version of compiler:
Intel® C++ Composer XE 2013 SP1 Update 1 Integration for Microsoft* Visual Studio* 2012, Version 14.0.1278.11
I know en C++03 is not possible to instanciate non-integral object like that. But I would like to know if it is possible with C++11. For me everything is "parsable" at the compilation time.
The question: It is a C++11 limitation OR Compiler issue?
Thanks
Link Copied
You are not using correct syntax for initializing a static data member.
The GNU compiler gives similar errors, i.e.:
sptxl8-72> g++48 -std=c++11 -c ex2.cpp
ex2.cpp:28:61: error: A<Type>::Func0 is not a type
static constexpr Object< Type > myObject( A< Type >::Func0(), A< Type >::Func1() );
^
ex2.cpp:28:81: error: A<Type>::Func1 is not a type
static constexpr Object< Type > myObject( A< Type >::Func0(), A< Type >::Func1() );
^
The initialization needs to be done with this syntax:
#ifdef OK // valid
static constexpr Object< Type > myObject = Object<Type>( A< Type
>::Func0(), A< Type >::Func1() );
#else // not valid
static constexpr Object< Type > myObject( A< Type >::Func0(), A< Type
>::Func1() );
#endif
Unfortunately the above does not work with our 14.0 compiler (an error
message about "expression must have a constant value" will occur).
But it will compile with GNU and it works with our development compiler
which will become our next compiler release.
Judy
For more complete information about compiler optimizations, see our Optimization Notice.