Is there a way to get this code work, just like it does when calling a static function with the dot notation?
struct A{
static void f(){ }
typedef int t;
};
template<typename T> void f(){}
int main(){
A a;
a.f(); //legit
f<a.t>(); //‘a’ cannot appear in a constant-expression, ‘.’ cannot appear in a constant-expression
a.t somevar; //invalid use of ‘A::t’
f<a::t>(); //‘a’ cannot appear in a constant-expression
a::t somevar; //‘a’ is not a class, namespace, or enumeration
}
EDIT:
Guys, please read the question and test your code before posting. The point here is NOT to use A::t but “invoke” t through an instance of A, like you can do with static methods.
You have to use
A::tinstead ofa.tbecause the typedef is likestaticandais an instance ofA.EDIT: In contrast to what I said above, it is not always “like
static“. For static members, there is this special rule:Since a
typedefis not a static member, this syntax is invalid.Given the instance
aand not this syntactic sugar, the only way to gettis to get the type of it. C++11 gives us a tool for that:However, I see no practical use of it (ok, maybe in macros, but everyone knows that templates are better).