Select class constructor using enable_if

十年热恋 提交于 2019-12-17 08:32:31

问题


Consider following code:

#include <iostream>
#include <type_traits>

template <typename T>
struct A {
    int val = 0;

    template <class = typename std::enable_if<T::value>::type>
    A(int n) : val(n) {};
    A(...) { }

    /* ... */
};

struct YES { constexpr static bool value = true; };
struct NO { constexpr static bool value = false; };

int main() {
    A<YES> y(10);
    A<NO> n;
    std::cout << "YES: " << y.val << std::endl
              << "NO:  " << n.val << std::endl;
}

I want to selectively define constructor A::A(int) only for some types using enable_if. For all other types there is default constructor A::A(...) which should be the default case for compiler when substitution fails. However this makes sense for me compiler (gcc version 4.9.0 20130714) is still complaining

sfinae.cpp: In instantiation of 'struct A': sfinae.cpp:19:11:
required from here sfinae.cpp:9:5: error: no type named 'type' in
'struct std::enable_if'
A(int n) : val(n) {};

Is something like this possible for constructor? Is this possible with another constructor(s) (copy-constructor and move-constructor)?


回答1:


I think this can't work with a single defaulted template parameter, because its value needs to be resolved when the class template is instantiated.

We need to defer the substitution to the point of constructor template instantiation. One way is to default the template parameter to T and add an extra dummy parameter to the constructor:

template<typename U = T>
A(int n, typename std::enable_if<U::value>::type* = 0) : val(n) { }



回答2:


Usually this is done using an anonymous defaulted argument :

A(int n, typename std::enable_if<T::value>::type* = 0) : val(n) {};

You can not use template parameters from the class to SFINAE out methods. SO one way is to add a dummy type replacing int :

see: http://ideone.com/2Gnyzj

#include <iostream>
#include <type_traits>

template <typename T>
struct A {
    int val = 0;

    template<typename Integer
            ,typename  = typename std::enable_if<T::value && sizeof(Integer)>::type
            >
    A(Integer n) : val(n) {};

    A(...) {}
    /* ... */
};

struct YES { constexpr static bool value = true; };
struct NO { constexpr static bool value = false; };

int main() {
    A<YES> y(10);
    A<NO> n;
    std::cout << "YES: " << y.val << std::endl
              << "NO:  " << n.val << std::endl;
}

This works because you use a member template parameter to SFINAE out the constructor but the test is always true so it doesn't pollute your checks




回答3:


With C++20

You can achieve that simply by adding requires to the template:

template <typename U = T> requires U::value
A(int n) : val(n) { }

The requires clause gets a constant expression that evaluates to true or false deciding thus whether to consider this method in the overload resolution, if the requires clause is true, or ignore it otherwise.

Code: https://godbolt.org/z/CKTDFE



来源:https://stackoverflow.com/questions/17842478/select-class-constructor-using-enable-if

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!