0

I want to build a class with configurable type and difference type template parameters. The first solution compiles fine at VS2012Nov and g++4.7.2:

template <typename T,
    typename DT = decltype(T()-T())>
class A { };

But when I hides decltype(T()-T()) to additional template, VS still compiles it, but g++ not.

template < typename T >
struct Delta {
    typedef decltype( T() - T() ) Value;
};

template <typename T,
    typename DT = Delta<T>::Value >
class A { };

Why g++ doesn't support such syntax?

Alexander Mihailov
  • 1,154
  • 7
  • 15

1 Answers1

1

You're missing the typename keyword, to tell the compiler that the dependent name is a type:

template < typename T >
struct Delta {
    typedef decltype( T() - T() ) Value;
};

template <typename T,
    typename DT = typename Delta<T>::Value >
class A { };

Live example

For more info, see Where and why do I have to put the “template” and “typename” keywords?.

Community
  • 1
  • 1
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • @AlexanderMihailov MSVC doesn't properly implement two-phase name lookup - it defers *all* name lookup (and much other parsing) until instantiation time (that's why it generally takes longer to compile template code than e.g. GCC does). At the time it does the lookup, it doesn't need `typename` any more. And if they had the code to check that a `typename` is needed, they might use it to follow the standard in the first place. – Angew is no longer proud of SO Mar 05 '14 at 09:23