基于模板值的条件编译? [英] Conditional compilation based on template values?

查看:150
本文介绍了基于模板值的条件编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提出的问题:
在模板中键入条件



 
$ b

非常相似, includestdafx.h
#include< type_traits>


class AA {
public:
double a;

double Plus(AA& b){
return a + b.a;
}
};

template< class T> double doit(T& t){
if(std :: is_same< T,AA> :: value)
return t.Plus
else
return t + t;
}

int _tmain(int argc,_TCHAR * argv [])
{
double a;
AA aa;

doit(a);
doit(aa);

return 0;
}

这不会编译,我也不期望它。这样的东西可能吗?根据模板值,我想要一些代码编译,而其他的不是。这里,'double'没有称为Plus的方法,类AA不覆盖+运算符。当考虑操作的微妙语义时,操作符重载并不总是可取的,所以我在寻找一个替代。我更喜欢做#ifdef的(真正的条件编译在ref'd问题提出),但基于模板值。

解决方案

  double doit(AA& t){
return t.Plus(t);;
}
template< class T> double doit(T& t){
return t + t;
}

您的代码不工作,因为如果模板被推导为 AA 然后 t + t 在体内是不成形的。另一方面,如果 T 被推导为 double ,则 t.Plus / code>变得不合格。



为了更好地理解发生了什么:
每个模板类型的模板被实例化。 / p>

doIt(a)实例化 doIt $ c> T = double

  double doIt< double>(double& t){ 
if(false)
return t.Plus(t); //< - 语法错误
else
return t + t;
}

doIt(aa)实例化 doIt T = AA

  double doIt< AA>(AA& t){
if(true)
return t.Plus
else
return t + t; //<语法错误
}






由于函数超载,您应避免专门设计函数模板。您可以阅读这篇精彩的Herb Sutter文章:为什么不专业化功能模板?


the question posed in: Type condition in template

is very similar, yet the original question wasn't quite answered.

#include "stdafx.h"
#include <type_traits>


class AA {
public:
    double a;

    double Plus(AA &b) {
        return a + b.a;
    }
};

template<class T> double doit(T &t) {
    if (std::is_same<T, AA>::value)
        return t.Plus(t);
    else
        return t + t;
}

int _tmain(int argc, _TCHAR* argv[])
{
    double a;
    AA aa;

    doit(a);
    doit(aa);

    return 0;
}

This doesn't compile, nor did I expect it to. Is something like this possible? Being, based on the template value, I want some code to be compiled, and others not. Here, 'double' doesn't have a method called "Plus" and class "AA" doesn't override the '+' operator. Operator overloading isn't always desirable when considering subtle semantics to the operations, so I'm looking for an alternative. I'd prefer to do #ifdef's (truly conditional compilation as posed in the ref'd question), but based on template values.

解决方案

double doit(AA &t) {
  return t.Plus(t);;
}
template<class T> double doit(T &t) {
  return t + t;
}

Your code doesn't work because if the template is deduced as AA then t + t inside the body is ill-formed. On the other hand if T is deduces as double then t.Plus(t) becomes ill-formed.

To better understand what is happening: A template is instantiated for each template type is called with.

doIt(a) instantiates doIt with T = double:

double doIt<double>(double &t) {
  if (false)
     return t.Plus(t);  // <-- syntax error
  else
    return t + t;
}

doIt(aa) instantiates doIt with T = AA:

double doIt<AA>(AA &t) {
  if (true)
    return t.Plus(t);
  else 
    return t + t;   // <-- syntax error
}


You should avoid specializing function templates because functions overload. You can read this excellent Herb Sutter article: Why Not Specialize Function Templates?

这篇关于基于模板值的条件编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆