Praveen
Praveen

Reputation: 9335

typedef and using in c++11

#include <iostream>

struct cls {
    using type = double;          //case 1
    // typedef double type;         //case 2
};

template<typename T>
void foo(typename T::type) {
    std::cout<<"T::type\n";
}

int main() {
    foo<cls>(22.2);
}

I believe using and be used instead of typedef. In the above code I'm getting error for case 1 but not for case 2.

error: expected nested-name-specifier before 'type'

Could someone please explain why??

Upvotes: 0

Views: 63

Answers (1)

Marco A.
Marco A.

Reputation: 43662

Your compiler either doesn't support C++11 type aliases, has limited support for them (MSVC used to incorrectly parse template types when not used) or hasn't the C++11 option active, make sure you're using a C++11 compatible (and enabled) one, i.e. upgrade your compiler.

struct cls {
    using type = double;         // Doesn't work on pre-C++11
    // typedef double type;      // Works on pre-C++11
};

I'm also recommending reading the difference between using and typedef in C++11

Upvotes: 2

Related Questions