简体   繁体   中英

Default template parameters

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?

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? .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM