C++ 如何使用可变模板转发类型和值模板参数?
假设我们有3种类型(S1、S2和S3): 它不适用于S2,因为已经定义了一个模板类型(在本例中,T4已经是int) 是否可以使用可变模板以这种方式匹配所有模板类型?C++ 如何使用可变模板转发类型和值模板参数?,c++,templates,c++14,variadic-templates,sfinae,C++,Templates,C++14,Variadic Templates,Sfinae,假设我们有3种类型(S1、S2和S3): 它不适用于S2,因为已经定义了一个模板类型(在本例中,T4已经是int) 是否可以使用可变模板以这种方式匹配所有模板类型? 不管它们是类型名还是实际类型。模板结构测试:std::true\u type{}; template <class A0,class A1, class A2, int x> struct test<S2<A0,A1,A2,x>>:std::true_type{}; 一般来说,使用非类型模板参
不管它们是类型名还是实际类型。
模板结构测试:std::true\u type{};
template <class A0,class A1, class A2, int x> struct test<S2<A0,A1,A2,x>>:std::true_type{};
一般来说,使用非类型模板参数进行元编程是一件痛苦的事情
如果你做了很多事情,可以考虑使用integral\u constant
之类的东西:将值作为类型传递,而不是作为值传递。您甚至可以将模板作为类型传递。模板结构测试:std::true\u type{};
一般来说,使用非类型模板参数进行元编程是一件痛苦的事情
如果你做了很多事情,可以考虑使用
integral\u constant
之类的东西:将值作为类型传递,而不是作为值传递。您甚至可以将模板作为类型传递。这里有一个解决方案,可以提供您想要的输出,而无需重新设计S2:
#include <iostream>
#include <type_traits>
template<typename T1, typename T2, typename T3>
struct S1{};
//type 2 (good type)
template<typename T1, typename T2, typename T3, int T4>
struct S2{};
//type 3 (bad type)
template<typename T1, typename T2, typename T3>
struct S3{};
template <typename T> struct test : std::false_type{};
template <typename... Args> struct test<S1<Args...>> : std::true_type{};
template <template <typename, typename, typename, int> class P, typename A, typename B, typename C, int N>
struct test<P<A,B,C,N>> : std::conditional<
std::is_same<P<A,B,C,N>, S2<A,B,C,N>>::value,
std::true_type,
std::false_type
>::type {};
template <typename T1, typename T2, typename T3, int T4> struct S2Wannabe {};
int main() {
std::cout << std::boolalpha;
std::cout << test<S1<int,int,int>>::value << '\n'; // true
std::cout << test<S2<int,int,int,5>>::value << '\n'; // true
std::cout << test<S3<int,int,int>>::value << '\n'; // false
std::cout << test<S2Wannabe<int,int,int,5>>::value << '\n'; // false
}
#包括
#包括
模板
结构S1{};
//类型2(良好类型)
模板
结构S2{};
//类型3(坏类型)
模板
结构S3{};
模板结构测试:std::false_type{};
模板结构测试:std::true_type{};
模板
结构测试:std::conditional<
std::值是否相同,
std::真_类型,
std::false_类型
>::类型{};
模板结构S2Wannabe{};
int main(){
std::cout这里有一个解决方案,可以提供您想要的输出,而无需重新设计S2:
#include <iostream>
#include <type_traits>
template<typename T1, typename T2, typename T3>
struct S1{};
//type 2 (good type)
template<typename T1, typename T2, typename T3, int T4>
struct S2{};
//type 3 (bad type)
template<typename T1, typename T2, typename T3>
struct S3{};
template <typename T> struct test : std::false_type{};
template <typename... Args> struct test<S1<Args...>> : std::true_type{};
template <template <typename, typename, typename, int> class P, typename A, typename B, typename C, int N>
struct test<P<A,B,C,N>> : std::conditional<
std::is_same<P<A,B,C,N>, S2<A,B,C,N>>::value,
std::true_type,
std::false_type
>::type {};
template <typename T1, typename T2, typename T3, int T4> struct S2Wannabe {};
int main() {
std::cout << std::boolalpha;
std::cout << test<S1<int,int,int>>::value << '\n'; // true
std::cout << test<S2<int,int,int,5>>::value << '\n'; // true
std::cout << test<S3<int,int,int>>::value << '\n'; // false
std::cout << test<S2Wannabe<int,int,int,5>>::value << '\n'; // false
}
#包括
#包括
模板
结构S1{};
//类型2(良好类型)
模板
结构S2{};
//类型3(坏类型)
模板
结构S3{};
模板结构测试:std::false_type{};
模板结构测试:std::true_type{};
模板
结构测试:std::conditional<
std::值是否相同,
std::真_类型,
std::false_类型
>::类型{};
模板结构S2Wannabe{};
int main(){
标准::cout
true
false
false
template <class A0,class A1, class A2, int x> struct test<S2<A0,A1,A2,x>>:std::true_type{};
#include <iostream>
#include <type_traits>
template<typename T1, typename T2, typename T3>
struct S1{};
//type 2 (good type)
template<typename T1, typename T2, typename T3, int T4>
struct S2{};
//type 3 (bad type)
template<typename T1, typename T2, typename T3>
struct S3{};
template <typename T> struct test : std::false_type{};
template <typename... Args> struct test<S1<Args...>> : std::true_type{};
template <template <typename, typename, typename, int> class P, typename A, typename B, typename C, int N>
struct test<P<A,B,C,N>> : std::conditional<
std::is_same<P<A,B,C,N>, S2<A,B,C,N>>::value,
std::true_type,
std::false_type
>::type {};
template <typename T1, typename T2, typename T3, int T4> struct S2Wannabe {};
int main() {
std::cout << std::boolalpha;
std::cout << test<S1<int,int,int>>::value << '\n'; // true
std::cout << test<S2<int,int,int,5>>::value << '\n'; // true
std::cout << test<S3<int,int,int>>::value << '\n'; // false
std::cout << test<S2Wannabe<int,int,int,5>>::value << '\n'; // false
}