c++ std::enable_if

2020-06-25 04:32发布

I am quite new to std::enable_if and wondering how to use it. I have a template class:

template<int a, int b>
class foo {
}

Now I only want to instantiate it when a + b equals to 10. Could I make this possible using std::enable_if?

The second question: If I have a member in class foo

template<int a, int b>
class foo {
  int c;
}

I only want to have c when

a = 5. 

How do I do that using std::enable_if? Is this one the correct case to use std::enable_if?

标签: c++ c++11
4条回答
Animai°情兽
2楼-- · 2020-06-25 05:09

I guess you can use static_assert better to enforce that constraint instead of enable_if

template<int a, int b>
class foo {
    static_assert(a+b==10, "a+b is not 10");
};

int main()
{
    foo<5,5> f; // will compile
    foo<1,5> f; // will fail to compile with error: a+b is not 10
    return 0;
}

enable_if is primarily used to conditionally remove functions and classes from overload resolution based on type traits and to provide separate function overloads and specializations for different type traits.

查看更多
我只想做你的唯一
3楼-- · 2020-06-25 05:15

With C++20

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

template<int a, int b> requires (a + b == 10)
struct foo {
    int c;
};

int main() {
    foo<3, 7> ok;
    // foo<3, 8> doesnt_compile;
}

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/yHh4Et

查看更多
萌系小妹纸
4楼-- · 2020-06-25 05:18
template<int a, int b, typename T = typename std::enable_if<a + b == 10>::type>
class foo {
};

This should do the job; just make sure you never explicitly provide the third template parameter when instantiating the template.


As others mentioned, static_assert is a much better fit.

查看更多
地球回转人心会变
5楼-- · 2020-06-25 05:30

Simple, just don't use enable_if

template<bool Ten>
struct Base
{
    int c;
};

template<>
struct Base<false>
{ };

template<int a, int b>
struct foo : Base<a+b == 10>
{
};
查看更多
登录 后发表回答