Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Type traits in constructor causing error [duplicate]

Possible Duplicate:
Where and why do I have to put the “template” and “typename” keywords?

I want to have a constructor that takes a single argument and is only enabled if the type of that argument has a member type ::t which must be a subtype of some other type. I am using type traits for this and the code looks like this:

#include <type_traits>

struct Y{};

struct X{
    //Only allow if T has a type member T::t which is a subtype of Y
    template <typename T>
    X(T* t, std::enable_if<std::is_base_of<Y, typename T::t>::value, int>::type e = 0){}
};

However, g++ complains the following:

test/test.cpp:8:75: error: ‘std::enable_if<std::is_base_of<Y, typename T::t>::value, int>::type’ is not a type

What have I done wrong?

like image 594
gexicide Avatar asked Apr 17 '26 10:04

gexicide


1 Answers

You have to add a typename to std::enable_if<...>::type to resolve this...

like image 114
Paul Michalik Avatar answered Apr 20 '26 15:04

Paul Michalik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!