For every template type an argument of a set type

。_饼干妹妹 提交于 2019-12-04 06:23:42
template <typename... Types>
class Test
{
    template <typename>
    using int_t = int;

public:    
    void Func(int_t<Types>... ints)
    {
    }
};

DEMO

wandbox example - (works with C++11)


If you do not require SFINAE, you can use static_assert to make sure your conditions are met:

template <typename... Types>
class Test
{
public:
    template <typename... Ts>
    void Func(Ts...)
    {
        static_assert(sizeof...(Ts) == sizeof...(Types), "");
        static_assert(std::conjunction<std::is_same<Ts, int>...>{}, "");
    }
};

(If you need SFINAE, use std::enable_if.)

std::conjunction checks that all the conditions passed to it are true.


With the above example, the following calls are valid/invalid:

myTest.Func(905, 36, 123315); // valid
myTest.Func(905, 36, 123315.f); // invalid
myTest.Func(905, 22); // invalid

As you can see, implicit conversions are not allowed with this solution. You could use std::is_convertible instead of std::is_same if you want them to be allowed.

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