Why can't a class have a decltype
in the inheritance list? For instance, I would expect the following code to make A<B>
inherit from RType
, but with G++ 4.6.1 (using -std=c++0x
) it does not compile:
#include <type_traits>
template<typename T>
class A : public decltype(std::declval<T>().hello()) { };
class RType { };
class B {
public:
RType hello() { return RType(); }
};
int main() {
A<B> a;
}
It gives the following output:
test.cpp:6:18: error: expected class-name before 'decltype'
test.cpp:6:18: error: expected '{' before 'decltype'
test.cpp:6:54: error: expected unqualified-id before '{' token
test.cpp: In function 'int main()':
test.cpp:16:7: error: aggregate 'A<B> a' has incomplete type and cannot be defined
The use of declval
is just to provide an instance where you needed to use decltype
, but other uses of decltype
also fail (that is, without declval
).
The decltype type specifier yields the type of a specified expression. The decltype type specifier, together with the auto keyword, is useful primarily to developers who write template libraries. Use auto and decltype to declare a template function whose return type depends on the types of its template arguments.
decltype returnsIf what we pass to decltype is the name of a variable (e.g. decltype(x) above) or function or denotes a member of an object ( decltype x.i ), then the result is the type of whatever this refers to.
It's allowed :
10.1 : "A list of base classes can be specified in a class definition using the notation:"
class-or-decltype:
nested-name-specifieropt class-name
decltype-specifier
so I guess your compiler has bug
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With