Select class constructor using enable_if [duplicate]
Asked Answered
M

3

69

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

Meningitis answered 24/7, 2013 at 18:51 Comment(1)
This question's closure is being discussed on Meta cc @JanSchultkeEveryman
M
27

With C++20

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

A(int n) requires T::value : 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/948z41zKK

Moya answered 15/11, 2019 at 12:40 Comment(0)
D
58

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) { }
Decommission answered 24/7, 2013 at 19:3 Comment(10)
better explanation than mine :DLocklin
@JoelFalcou Yours actually works. This one doesn't stillMindoro
@Mindoro My bad that's because it's used in a default template parameter. It needs to be in constructor's signature. I'll fix it.Decommission
Yes, this is very elegant. I'm afraid that this does not work. Could you give us explanation why even if it should as you described in your's answer?Meningitis
@Decommission Even if it is on the constructor's signature (as noted in a previous answer) it will not work.Mindoro
@Mindoro Yes, I was completely off, thanks for heads up. It's fixed now.Decommission
@Decommission It's more common to put it in the template parameters, but yes you're right that this isn't possible with a single template parameter (that's actually the answer to the question).Mindoro
I'm curious about the overhead of passing in the dummy parameter. Would most compilers be smart enough to optimise that out if this was in a header file?Tetzel
@Tetzel So was I, so I tested on gcc 9.3.0 (mingw). Without -O it does not optimize it out. With -O1 it drops it, and also inlines the entire constructor. (Based on examining decompiled bin).Breaststroke
this is genious, thanks! i use the following hybrid without an extra parameter: template <typename U = T, class = typename std::enable_if<U::value>::type>Prostate
M
27

With C++20

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

A(int n) requires T::value : 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/948z41zKK

Moya answered 15/11, 2019 at 12:40 Comment(0)
L
14

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

Locklin answered 24/7, 2013 at 18:53 Comment(9)
That still gives the same error, and I was under the impression the OP's method was the most common one.Maun
@Maun It actually is.Mindoro
It is in C++11. I just noticed the std::enable_if.Locklin
This solution is perfectly working. Small defect is it changes function signature. Is there a solution with same signature? Like @jrok's?Meningitis
@Meningitis You can use another type-trait, as seen hereMindoro
@Mindoro Very clever idea. It looks great. Thanks. I think it is more obvious what it should do.Meningitis
is_convertible is the bets shot. I just used sizeof as a "always true" condition but you may want to assert the type indeedLocklin
@Mindoro I have revised your's code at link and I do not fully understand those three dots at the end of enable_if . Could you please further describe why are there?Meningitis
@Meningitis It's just to avoid writing typename = or class = before it.Mindoro

© 2022 - 2024 — McMap. All rights reserved.