强制使用某种类型的可变参数模板

时间:2015-05-20 10:11:15

标签: c++ templates c++11 variadic-templates

我想强制使用可变参数模板的类型与先前设置的模板类型相同。在下面的例子中,我希望T和U是相同的类型。

code on ideone.com

#include <iostream>
#include <string>

template<class T>
struct Foo {

    Foo(T val) {
        std::cout << "Called single argument ctor" << std::endl;
        // [...]    
    }    

    // How to enforce U to be the same type as T?
    template<class... U>
    Foo(T first, U... vals) {
        std::cout << "Called multiple argument ctor" << std::endl;
        // [...]   
    }

};

int main() {

    // Should work as expected.
    Foo<int> single(1);

    // Should work as expected.
    Foo<int> multiple(1, 2, 3, 4, 5);

    // Should't work (but works right now). The strings are not integers.
    Foo<int> mixedtype(1, "a", "b", "c");

    // Also shouldn't work. (doesn't work right now, so that is good)
    Foo<int> alsomixedtype(1, 1, "b", "c");
}

5 个答案:

答案 0 :(得分:12)

我们可以使用SFINAE确保所有U类型与T相同。需要注意的一点是,U不仅仅是您所暗示的一种类型,而是可能不同类型的列表。

template<class... U, std::enable_if_t<all_same<T, U...>::value>* = nullptr>
Foo(T first, U... vals) {
    std::cout << "Called multiple argument ctor" << std::endl;
    // [...]   
}

std::enable_if_t来自C ++ 14。如果这不适合您,请使用std::enable_if

typename std::enable_if<all_same<T, U...>::value>::type* = nullptr>

all_same可以通过多种不同的方式实现。这是我喜欢使用布尔包的方法:

namespace detail
{
    template<bool...> struct bool_pack;
    template<bool... bs>
    //if any are false, they'll be shifted in the second version, so types won't match
    using all_true = std::is_same<bool_pack<bs..., true>, bool_pack<true, bs...>>;
}
template <typename... Ts>
using all_true = detail::all_true<Ts::value...>;

template <typename T, typename... Ts>
using all_same = all_true<std::is_same<T,Ts>...>;

答案 1 :(得分:2)

std::conjunction(逻辑与)是C ++ 17中引入的,因此不再需要手动实现all_same。然后,构造函数将变得简单:

template<typename... U,
    typename = std::enable_if_t<
        std::conjunction_v<
            std::is_same<T, U>...
        >
    >
>
Foo(T first, U... vals)
{
    std::cout << "Called multiple argument ctor" << std::endl;
    // [...]   
}

请参见live example

答案 2 :(得分:1)

C++20 概念让它变得如此简单

    template<std::same_as<T>... U>
    Foo(T first, U... vals) {
        std::cout << "Called multiple argument ctor" << std::endl;
        // [...]   
    }

https://gcc.godbolt.org/z/neEsvo

答案 3 :(得分:0)

如果要求所有参数都是相同类型且参数数量可变,

对于这些要求,

可变参数模板太重,只需使用 C ++ 11 std :: initializer_list

如果您可以在通话中将()替换为 {} ,他们就可以胜任。

template<class T> struct Foo {
    Foo(T val) {
        std::cout << "Called single argument ctor" << std::endl;
    }
    // Enforce all parameters to be the same type :
    Foo( std::initializer_list<T> values ) {
        std::cout << "Called multiple argument ctor" << std::endl;
        for (T value : values)
            cout << value << endl;
    }
};

int main() {
    // Work as expected.
    Foo<int> single(1);
    // Work as expected.
    Foo<int> multiple{ 1, 2, 3, 4, 5 };
    // Doesn't work - as required :
    //Foo<int> mixedtype{ 1, "a", "b", "c" };

}

答案 4 :(得分:-1)

如果不实现all_same,您还可以按如下方式更改构造函数代码:

template<class F, typename = typename enable_if<is_same<F, T>::value>::type, class... U>
    Foo(F first, U... vals): Foo(vals...) {
    std::cout << "Called multiple argument ctor" << std::endl;
    // [...]   
}

is_same是STL <type_traits>

中的函数