【问题标题】:Boost::typeof compiler problem: mangling typeof, use decltype insteadBoost::typeof 编译器问题:修改 typeof,改用 decltype
【发布时间】:2009-11-05 15:54:09
【问题描述】:

简短示例:

#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

【问题讨论】:

  • 也许如果你的编译器支持它,使用 C++0x 特性:auto add2(const T&amp; t, const U&amp; u) -&gt; decltype(t + u)

标签: c++ templates boost typeof


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-14
    • 2012-12-17
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多