小编Chr*_*ris的帖子

C++ 0x auto,decltype和模板函数

我一直在阅读上的C++ 0x CodeProject上的文章,并给它一个快速尝试在VC2010.但是我遇到了编译错误,我对这个问题有点不知所措.

#include < iostream>

template <typename FirstType, typename SecondType>
auto  AddThem(FirstType t1, SecondType t1) -> decltype(t1 + t2)
{
    return t1 + t2;
}

int main()
{

    auto a = 3.14;
    auto b = 3;
    auto c = AddThem<decltype(a),decltype(b)>(a,b);
    std::cout << c << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

结果出现此错误:

错误C2086:'FirstType t1':重新定义1> main.cpp(4):参见't1'的声明1> main.cpp(14):错误C2780:''unknown-type'AddThem(FirstType)':期望1参数 - 2提供1>
main.cpp(4):参见'AddThem'1> main.cpp(14)的声明:致命错误C1903:无法从先前的错误中恢复; 停止编译

谢谢你的任何想法.

c++ c++11

2
推荐指数
1
解决办法
778
查看次数

加速boost :: iterator_facade取消引用

我正在使用iterator_facade为类提供迭代器支持.但是,因为iterator_facade :: dereference通过引用返回,并且它是一个返回非平凡copy-const的对象,所以我的性能会因为不断执行这些而导致每次取消引用(如使用VTune进行分析)副本.有没有解决的办法?

class time_series::iterator : public boost::iterator_facade<
                         time_series::iterator,
                         time_series::variable_vec,
                         boost::bidirectional_traversal_tag,
                         timestep> //<--- dereference type
{
public:
    iterator();
    iterator(const iterator& src);
    ~iterator();
    iterator& operator=(const iterator& rhs);
private:
     //the following satisfies the reqs for a boost::facade bidirectional iterator
     friend class boost::iterator_core_access;
     friend class time_series;

     const timestep& dereference() const;
     bool equal(iterator const& other) const;
     void increment();
     void decrement();
     std::ptrdiff_t distance_to(iterator const& other) const;

     //iterators for the current step
     timestep _currentStep;// <--returned on dereference, is non-trivial to copy.
};
Run Code Online (Sandbox Code Playgroud)

编辑:根据评论

const timestep& time_series::iterator::dereference() …
Run Code Online (Sandbox Code Playgroud)

c++ performance boost iterator

1
推荐指数
1
解决办法
812
查看次数

标签 统计

c++ ×2

boost ×1

c++11 ×1

iterator ×1

performance ×1