c++ - Boost::typeof 编译器问题:修改 typeof,使用 decltype 代替

标签 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(){

     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;
 }

当我尝试编译它时,我出现如下错误:

g++-4.3:抱歉,未实现:修改 typeof,改用 decltype

g++-4.2:internal compiler error: in write_type, at cp/mangle.c:1648 Please submit a full bug report, with preprocessed source if appropriate. See <URL:http://gcc.gnu.org/bugs.html> for instructions. For Debian GNU/Linux specific bug reporting instructions, see <URL:file:///usr/share/doc/gcc-4.2/README.Bugs>.

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

问题出在哪里或者我做错了什么?a

最佳答案

The example in the typeof documentation首先将 BOOST_TYPEOF_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;
};

关于c++ - Boost::typeof 编译器问题:修改 typeof,使用 decltype 代替,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1681459/

相关文章:

c++ - 运算符 = 不明确 (C++)

c++ - 在模板派生类中禁用方法覆盖

c++ - 错误: ambiguous call to overloaded function

c++ - BlackBerry 10开发-TextField

c# - 将 Ctrl+Up 发送到窗口

c++ - GCC和Clang中模板类如何继承子类

c++ - 优化 boost::spirit::qi 解析器

C++ 在非内联函数中使用 typedef

templates - 如何在 Go html/template 中获取 map 元素的结构字段?

c++ - 在结构中初始化数组