强制执行某种类型的可变参数模板

Enforce variadic template of certain type

我想强制可变参数模板的类型与早先设置的模板类型相同。在下面的示例中,我希望 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");
}

我们可以使用 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>...>;

如果不实现 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>

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

Variadic 模板太重 对于这些要求,只需使用 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" };

}

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

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