0
#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??

Praveen
  • 8,945
  • 4
  • 31
  • 49

1 Answers1

2

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

Community
  • 1
  • 1
Marco A.
  • 43,032
  • 26
  • 132
  • 246