Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't a class inherit from the result of a decltype?

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).

like image 571
Seth Carnegie Avatar asked Feb 20 '12 21:02

Seth Carnegie


People also ask

What does decltype do in c++?

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.

What does decltype return?

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.


1 Answers

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

like image 161
Mr.Anubis Avatar answered Sep 30 '22 19:09

Mr.Anubis