Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为4种类型选择2个模板函数之一_C++_Templates_C++11 - Fatal编程技术网

C++ 为4种类型选择2个模板函数之一

C++ 为4种类型选择2个模板函数之一,c++,templates,c++11,C++,Templates,C++11,在本例中,是否有一种更干净(“较少的c++11”)的方法根据多种类型选择要使用的模板函数。我只想要2个函数,我可以用额外的2或4个函数重载或调用实现 struct A{}; struct B{}; struct C{}; struct D{}; template <typename T> typename std::enable_if<std::is_same<T, A>::value or std::is_same<T, B>::value, voi

在本例中,是否有一种更干净(“较少的c++11”)的方法根据多种类型选择要使用的模板函数。我只想要2个函数,我可以用额外的2或4个函数重载或调用实现

struct A{}; struct B{}; struct C{}; struct D{};

template <typename T>
typename std::enable_if<std::is_same<T, A>::value or std::is_same<T, B>::value, void>::type
foo(T& t)
{ 
    printf("first\n"); 
}

template <typename T>
typename std::enable_if<std::is_same<T, C>::value or std::is_same<T, D>::value, void>::type
foo(T& t)
{ 
    printf("second\n"); 
}

int main()
{
  A a; B b; C c; D d;
  foo(a); foo(b); foo(c); foo(d);
}
struct A{};结构B{};结构C{};结构D{};
样板
typename std::enable_if::type
傅(T&T)
{ 
printf(“第一次”);
}
样板
typename std::enable_if::type
傅(T&T)
{ 
printf(“第二次”);
}
int main()
{
A A,B,C,D,;
foo(a);foo(b);foo(c);foo(d);
}

有一种过载方式:

void foo(A& a) { printf("first\n"); }
void foo(B& b) { printf("first\n"); }
void foo(C& c) { printf("second\n"); }
void foo(D& d) { printf("second\n"); }
或者避免代码重复

template <typename T> void print_first(T&) { printf("first\n"); }
template <typename T> void print_second(T&) { printf("second\n"); }

void foo(A& a) { print_first(a); }
void foo(B& b) { print_first(b); }
void foo(C& c) { print_second(c); }
void foo(D& d) { print_second(d); }
template void print_first(T&){printf(“first\n”);}
模板无效打印_second(T&){printf(“second\n”);}
void foo(A&A){print_first(A);}
void foo(B&B){print_first(B);}
void foo(C&C){print_second(C);}
void foo(D&D){print_second(D);}

有一种过载方式:

void foo(A& a) { printf("first\n"); }
void foo(B& b) { printf("first\n"); }
void foo(C& c) { printf("second\n"); }
void foo(D& d) { printf("second\n"); }
或者避免代码重复

template <typename T> void print_first(T&) { printf("first\n"); }
template <typename T> void print_second(T&) { printf("second\n"); }

void foo(A& a) { print_first(a); }
void foo(B& b) { print_first(b); }
void foo(C& c) { print_second(c); }
void foo(D& d) { print_second(d); }
template void print_first(T&){printf(“first\n”);}
模板无效打印_second(T&){printf(“second\n”);}
void foo(A&A){print_first(A);}
void foo(B&B){print_first(B);}
void foo(C&C){print_second(C);}
void foo(D&D){print_second(D);}

为什么不能利用继承,例如:

#include <iostream>

template <size_t>
struct tag {};

struct A: tag<1>{ }; struct B: tag<1>{ };
struct C: tag<2>{ }; struct D: tag<2>{ };

void foo(tag<1>&) {
    std::cout << "1" << std::endl;
}

void foo(tag<2>&) {
    std::cout << "2" << std::endl;
}

int main() {
   A a; B b; C c; D d;
   foo(a);  foo(b); foo(c); foo(d);
}

为什么你不能仅仅利用继承权,例如:

#include <iostream>

template <size_t>
struct tag {};

struct A: tag<1>{ }; struct B: tag<1>{ };
struct C: tag<2>{ }; struct D: tag<2>{ };

void foo(tag<1>&) {
    std::cout << "1" << std::endl;
}

void foo(tag<2>&) {
    std::cout << "2" << std::endl;
}

int main() {
   A a; B b; C c; D d;
   foo(a);  foo(b); foo(c); foo(d);
}
更少的c++11

下面的代码符合C++98修订版。
它模拟了更多的C++11 ish
std::enable_if
,仅此而已

#include<cstdio>

struct A{}; struct B{}; struct C{}; struct D{};

template<typename> struct R1;
template<> struct R1<A> { typedef void type; };
template<> struct R1<B> { typedef void type; };

template<typename> struct R2;
template<> struct R2<C> { typedef void type; };
template<> struct R2<D> { typedef void type; };

template <typename T>
typename R1<T>::type foo(T& t)
{ 
    printf("first\n"); 
}

template <typename T>
typename R2<T>::type foo(T& t)
{ 
    printf("second\n"); 
}

int main()
{
    A a; B b; C c; D d;
    foo(a); foo(b); foo(c); foo(d);
}
#包括
结构A{};结构B{};结构C{};结构D{};
模板结构R1;
模板结构R1{typedef void type;};
模板结构R1{typedef void type;};
模板结构R2;
模板结构R2{typedef void type;};
模板结构R2{typedef void type;};
样板
类型名称R1::类型foo(T&T)
{ 
printf(“第一次”);
}
样板
类型名称R2::类型foo(T&T)
{ 
printf(“第二次”);
}
int main()
{
A A,B,C,D,;
foo(a);foo(b);foo(c);foo(d);
}
清洁剂

老实说,我发现C++11更干净,它的
使用了
声明和
std::enable_if

更少的c++11

下面的代码符合C++98修订版。
它模拟了更多的C++11 ish
std::enable_if
,仅此而已

#include<cstdio>

struct A{}; struct B{}; struct C{}; struct D{};

template<typename> struct R1;
template<> struct R1<A> { typedef void type; };
template<> struct R1<B> { typedef void type; };

template<typename> struct R2;
template<> struct R2<C> { typedef void type; };
template<> struct R2<D> { typedef void type; };

template <typename T>
typename R1<T>::type foo(T& t)
{ 
    printf("first\n"); 
}

template <typename T>
typename R2<T>::type foo(T& t)
{ 
    printf("second\n"); 
}

int main()
{
    A a; B b; C c; D d;
    foo(a); foo(b); foo(c); foo(d);
}
#包括
结构A{};结构B{};结构C{};结构D{};
模板结构R1;
模板结构R1{typedef void type;};
模板结构R1{typedef void type;};
模板结构R2;
模板结构R2{typedef void type;};
模板结构R2{typedef void type;};
样板
类型名称R1::类型foo(T&T)
{ 
printf(“第一次”);
}
样板
类型名称R2::类型foo(T&T)
{ 
printf(“第二次”);
}
int main()
{
A A,B,C,D,;
foo(a);foo(b);foo(c);foo(d);
}
清洁剂


老实说,我发现C++11更干净,它的
使用
声明和
std::enable\u如果

对不起,我应该提到,我只需要2个函数,更新了问题。@Salgar 4个函数有什么问题?如果你想避免代码重复,你可以制作2个模板函数,让重载调用其中一个函数。这是6个函数,听起来比我上面的代码要多得多?但是没有“复杂的”
std::enable_If
和traits。对不起,我应该提到,我只想要2个函数,更新了问题。@Salgar 4个函数有什么问题?如果您想避免代码重复,可以创建2个模板函数,并让重载调用其中一个函数。这是6个函数,听起来比我上面的代码多得多?但是没有“复杂的”
std::enable_If
和traits。不,是显式的,更新了问题。您可以创建一个traits来缩短
(以检查
T
是否在类型列表{
a
B
)中),但它更像是c++11。。你想解决的真正问题是什么。不,不是你要问的问题,而是你认为其解决方案就是你要问的问题。不,是明确的,更新了问题。你可以创建一个traits来缩短
(检查
T
是否在类型列表中{
a
B
),但更多的是c++11。。你想解决的真正问题是什么。不,不是你要问的问题,而是你所要问的问题是你相信谁的解决方案。