Jump to content
  • Advertisement
Sign in to follow this  
blubberbert

C++ template default parameter with enable_if

This topic is 2162 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

my question is simple (i think)

 

consider this

class Base { ... };
 
class A {
public:
  template <class T = std::enable_if<std::is_base_of<Base, T>::value>::type>
  void Test1() { }

  template <class T, T = std::enable_if<std::is_base_of<Base, T>::value>::type>
  void Test2() { }

};
 
a.Test1<int>(); //compiles
a.Test2<int>(); //doesn't compile

why does the Test1 call compile when it shouldnt, because int isnt derived from Base

ive already found the correct syntax (wich seems to be the one of Test2)

but i dont get why the enable_if doesnt create an error in Test1

 

by the way im using Visual Studio

Share this post


Link to post
Share on other sites
Advertisement
Disclaimer: Inexperienced in generic programming.

Hmm? Test1 works, because T=int, bypassing the default template parameter. Not sure about the exact reasons for Test2, other than T is declared twice. I wasn't able to work around it:


#include <type_traits>
#pragma warning(1 : 4519)

class Base { };
 
class A {
public:
	// C4519
	template <class T = std::enable_if<std::is_base_of<Base, T>::value>::type>
	void Test1() { }

	// C4519
	template <class T, typename G = std::enable_if<std::is_base_of<Base, T>::value>::type>
	void Test2() { }
};

int main() { 
	A a;
	a.Test1<int>();		// Ok (bad)
	a.Test2<Base>();	// C2783:  Could not deduce template argument for G
	a.Test2<int>();		// C2783:  Could not deduce template argument for G
}
However, this appears to work:
#include <type_traits>

class Base {
public:
    virtual ~Base() {
    }
};

class Derived : Base {
};

class A {
private:
    template<typename T>
    void ff() {
    }
    
public:
    template<typename T>
    void f() {
        typedef std::enable_if<std::is_base_of<Base,T>::value,T>::type Q;
        ff<Q>();
    }
};

int main() {
    A a;
    a.f<int>();     // error C2039: 'type' is not a member of enable_if<false,int>
    a.f<Base>();    // ok
    a.f<Derived>(); // ok
}
Edited by fastcall22

Share this post


Link to post
Share on other sites

Never used enable_if before but according to http://en.cppreference.com/w/cpp/types/enable_if it should be

class Base

{};
 
class A

{

public:

    template <class T>

    typename std::enable_if<std::is_base_of<Base, T>::value>::type Test1()

    {}
 
    // C++14

    template <class T>

    std::enable_if_t<std::is_base_of<Base, T>::value> Test2()

    {}

};
 
int main()

{

    A a;

    a.Test1<Base>();

    return 0;

}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!