Boost :: enable_if_c doesn't seem to work

Possible duplicate:
How to use enable_if to enable member functions based on a class template parameter

I have a class template:

template<typename T, size_t N> class Vector 

I want to include constructors for a specific N , so I do:

 Vector(typename boost::enable_if_c<N==2, T>::type const &e0, T const &e1) { data[0] = e0; data[1] = e1; } 

But the compiler (MSVC 2010 SP1) gives me an error instead of using SFINAE. Error:

 error C2039: 'type' : is not a member of 'boost::enable_if_c<B,T>' with [ B=false, T=float ] 

What is the problem? Is this a known issue? How can i fix this? Is this the only solution to use static_assert ?

Edit: GCC also fails: http://ideone.com/7Ejo8

+6
source share
1 answer

You cannot use enable_if to enable / disable member functions based on class template parameters: enable_if can only be applied to function or class templates.

In your case, the only solution I can think of is specializing in the whole class using enable_if or more simply with partial specialization. You can put common members in a common base class so you don't have to repeat them:

 #include <cstdio> template<typename T, std::size_t N> struct VectorCommon { std::size_t size() { return N; } void add(T const & element) { } }; template <typename T, std::size_t N> struct Vector : VectorCommon<T, N> { }; template <typename T> struct Vector<T, 2> : VectorCommon<T, 2> { Vector(T const & e0, T const & e1) {} }; int main() { //Vector<int, 100> v100(12, 42); // compile error Vector<char, 2> v2('a', 'b'); // ok } 
+5
source

Source: https://habr.com/ru/post/911761/


All Articles