VS2010为什么会为以下代码生成语法错误

时间:2019-06-26 18:47:13

标签: c++ visual-studio-2010 lazy-initialization

以下代码在VS2013及更高版本以及GCC下可以正常编译

https://godbolt.org/z/-nKXB-

#include "boost/thread/once.hpp"
#include "boost/optional.hpp"
#include <functional>
/// A class to encapsulate lazy initialization of an object.
/// All instances of Lazy are thread safe and the factory
/// is guaranteed only to be called once. However if you
/// use this for member initialization DO NOT capture
/// this in the lambda. See LazyMember for an alternative
template <typename T>
class Lazy {
    mutable boost::once_flag _once;
    mutable boost::optional<T> _data;
    typedef std::function<T()> FactoryFn;
    FactoryFn _factory;
    void Init() const { boost::call_once([&]() { _data = _factory(); }, _once); }
public:
    Lazy(FactoryFn factory):
        _once(BOOST_ONCE_INIT),
        _factory(factory)
        {

        }

    T& Value() {
        Init();
        return *_data;
    }
}; 

使用方式

Lazy<int> i([]{return 10;}); // Pretend lambda is an expensive op
// and then some time later.
return i.Value()

我在VS2010上进行测试

1>Lazy.hpp(26):
error C2059: syntax error : ')' 1>       
Lazy.hpp(37)
: see reference to class template instantiation 'mw::Lazy<T>' being
compiled
1>Lazy.hpp(26):
error C2059: syntax error : ','
1>Lazy.hpp(28):
error C2334: unexpected token(s) preceding '{'; skipping apparent
function body

似乎可以解决。

/// A class to encapsulate lazy initialization of an object.
/// All instances of Lazy are thread safe and the factory
/// is guaranteed only to be called once. However if you
/// use this for member initialization DO NOT capture
/// this in the lambda. See LazyMember for an alternative
template <typename T>
class Lazy {
    mutable boost::once_flag _once;
    mutable boost::optional<T> _data;
    typedef std::function<T()> FactoryFn;
    FactoryFn _factory;
    void Init() const { boost::call_once([&]() { _data = _factory(); }, _once); }
public:
    Lazy(FactoryFn factory):
        _factory(factory)
        {
          _once = BOOST_ONCE_INIT;
        }

    T& Value() {
        Init();
        return *_data;
    }

}; 

但是根本问题是什么?

0 个答案:

没有答案