Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++0x decltype and the scope resolution operator

With a class such as Foo:

struct Foo { static const int i = 9; };

I find that GCC 4.5 will reject the following

Foo f;
int x = decltype(f)::i;

It will work if I use an intermediate typedef, such as:

typedef decltype(f) ftype;
int x = ftype::i;

but I prefer to keep the namespace clean. I thought precedence may be an issue, so I've also tried parentheses, but no luck. Is it impossible as presented, or is there a piece of syntax that can help me?

like image 462
user2023370 Avatar asked Apr 02 '11 20:04

user2023370


People also ask

What is scope resolution operator in CPP?

The :: (scope resolution) operator is used to qualify hidden names so that you can still use them. You can use the unary scope operator if a namespace scope or global scope name is hidden by an explicit declaration of the same name in a block or class.

What is the use of scope resolution operator in inheritance?

Uses of the scope resolution Operator It defines the member function outside of the class using the scope resolution. It is used to access the static variable and static function of a class. The scope resolution operator is used to override function in the Inheritance.

What is the help of scope resolution in multiple inheritance?

In case of multiple Inheritance If you have two parent classes with same variable names and you are inheriting both of them in the child class, then you can use scope resolution operator with the class name to access the individual variables.


1 Answers

It is valid C++0x to say decltype(f)::i. GCC just doesn't support it yet. You can work it around with an identity template

template<typename T> struct identity { typedef T type; };
int x = identity<decltype(f)>::type::i;

identity is part of the boost::mpl namespace.

like image 116
Johannes Schaub - litb Avatar answered Oct 18 '22 08:10

Johannes Schaub - litb