简体   繁体   English

在编译期间检查模板类型参数以进行特定于类型的操作

[英]Check for template type parameter, during compile time, for type specific operation

First of all, the code is restricted to C++11, so I cannot make use of if constexpr首先,代码被限制在C++11,所以我不能使用if constexpr

Following is my sample code snippet:以下是我的示例代码片段:

class A{
 public:
    int a;
    int b;
}

class B{
 public:
    int key;
    int val;
}

class C{
 public:
    int n1;
    int n2;
}

class D{
 public:
    int n1;
    int n2;
}

class E{
 public:
    int n1;
    int n2;
}

template<typename T>
void func1(T data) {
   if (T == type(A)) {             // Just pseudo template-check code
    std::cout<<data.a<<data.b;              //<------1
   } else if (T == type (B)) {    // Just pseudo template-check code
    std::cout<<data.key<<data.val;          //<------2
   } else {
    std::cout<<data.n1<<data.n2;            //<------3
}


int main() {
A a;
B b;
C c;
D d;
E e;

func1(a);
func1(b);
func1(c);
func1(d);
func1(e);

return 0;
}

Currently, I get a compile-time error at,目前,我收到一个编译时错误,

1: B,D,E,F has no member a & b
 & 
2: A,D,E,F has no member key & val
 &
3. A, B has no member n1 & n2

I tried using is_same() & also this , but I get same compile time error every time.我尝试使用is_same()this ,但每次都得到相同的编译时错误。

  1. I cannot make use of C++14/C++17我无法使用 C++14/C++17
  2. How could I make use of specialized template functions?我怎样才能使用专门的模板功能?

Edited the code to highlight the need of a template.编辑代码以突出显示模板的需要。

You can use a function overload and avoid the function template altogether.您可以使用 function 过载并完全避免 function 模板。

void func1(A a)
{
   // Type dependent code.
}

void func1(B a)
{
   // Type dependent code.
}

A function template makes sense only if there is common code for all the types for which the function call is made.仅当调用 function 的所有类型都有公共代码时,function 模板才有意义。 If you have some code that is common to all types and some code that are type dependent, then you can use:如果您有一些对所有类型都通用的代码和一些依赖于类型的代码,那么您可以使用:

void func1(A a)
{
   // Type dependent code.
}

void func1(B a)
{
   // Type dependent code.
}

template <typename T>
void func2(T t)
{
   // Type independent code.
}

template <typename T>
void func(T obj)
{
   func1(obj);   // Call function that uses type dependent code.
   func2(obj);   // Call function that uses type independent code.
}

You must write specializations of the function for the two types your want to use it with.您必须为要使用的两种类型编写 function 的特化。

#include<iostream>

class A{
 public:
    int a;
    int b;
};

class B{
 public:
    int key;
    int val;
};

template<typename T>
void func1(T);

template<>
void func1<A>(A arg) {
    std::cout<<"A"<<std::endl;
    std::cout<<arg.a<<arg.b;
}

template<>
void func1<B>(B arg) {
    std::cout<<"B"<<std::endl;
    std::cout<<arg.key<<arg.val;
}

int main(){
A a;
func1(a);

B b;
func1(b);

}

Simple overload does the job.简单的重载就可以了。

template <typename T>
void func1(T data)
{
    std::cout << data.n1 << data.n2;
}

void func1(A data)
{
    std::cout << data.a << data.b;
}

void func1(B data)
{
    std::cout << data.key << data.val;
}

https://godbolt.org/z/r7Ee6E https://godbolt.org/z/r7Ee6E
Tweaked a bit: https://godbolt.org/z/xxPWaE稍作调整: https://godbolt.org/z/xxPWaE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM