C++ typeof编译器问题:损坏typeof,请改用decltype

C++ typeof编译器问题:损坏typeof,请改用decltype,c++,templates,boost,typeof,C++,Templates,Boost,Typeof,简短示例: #include <boost/typeof/typeof.hpp> #include <boost/proto/core.hpp> using namespace boost; template<class T, class U> BOOST_TYPEOF_TPL(T() + U()) add2(const T& t, const U& u) { return t + u; }; int main(){

简短示例:

#include <boost/typeof/typeof.hpp>
#include <boost/proto/core.hpp>
using namespace boost;


template<class T, class U>
BOOST_TYPEOF_TPL(T() + U()) add2(const T& t, const U& u)
{
    return t + u;
};

int main(){

     typedef BOOST_TYPEOF(add2(2.5, 1.5)) type; // get type -> works

     BOOST_STATIC_ASSERT((is_same<type, double>::value)); // check if double -> no error -> double

     double xxx = add2(1.5,1.5); // cause the problems
        return 0;
 }
#包括
#包括
使用名称空间boost;
模板
增压类型的TPL(T()+U())add2(常数T&T,常数U&U)
{
返回t+u;
};
int main(){
typedef BOOST_TYPEOF(add2(2.5,1.5))type;//获取类型->工作
BOOST_STATIC_ASSERT((is_same::value));//检查是否加倍->无错误->加倍
double xxx=add2(1.5,1.5);//导致问题
返回0;
}
当我试图编译此文件时,会出现如下错误:

g++-4.3:抱歉,未实现:损坏typeof,请改用decltype

g++-4.2:
内部编译器错误:以write_类型,位于cp/mangle.c:1648
请提交完整的bug报告,
如果合适,使用预处理源。
有关说明,请参阅。
对于Debian GNU/Linux特定的bug报告说明,
请参阅。

gcc版本4.3.2(Debian 4.3.2-1.1) gcc版本4.2.4(Debian 4.2.4-6)

问题出在哪里?我做错了什么?一个

首先将BOOST\u TYPEOF\u TPL宏的结果包装在一个模板结构中,然后在声明函数时使用它。这对你有好处吗

template<class T, class U>
struct result_of_add2
{
    typedef BOOST_TYPEOF_TPL(T() + U()) type;
};

template<class T, class U>
typename result_of_add2<T, U>::type add2(const T& t, const U& u)
{
    return t + u;
};
模板
添加2的结构结果\u
{
typedef BOOST_TYPEOF_TPL(T()+U())类型;
};
模板
类型名称结果\U of_add2::类型add2(常量T&T,常量U&U)
{
返回t+u;
};

如果您的编译器支持,可以使用C++0x功能:
autoadd2(const T&T,const U&U)->decltype(T+U)