C++ 构造函数可以';不接受非常量引用
我有以下课程:C++ 构造函数可以';不接受非常量引用,c++,gcc,boost,c++98,C++,Gcc,Boost,C++98,我有以下课程: class Foo { public: explicit Foo(std::vector<std::string>& functionCalls) { } }; typedef boost::shared_ptr<Foo> FooPtr; class-Foo { 公众: 显式Foo(std::vector和functionCalls) { } }; typedef boost::shared_ptr FooPtr; 我
class Foo
{
public:
explicit Foo(std::vector<std::string>& functionCalls)
{
}
};
typedef boost::shared_ptr<Foo> FooPtr;
class-Foo
{
公众:
显式Foo(std::vector和functionCalls)
{
}
};
typedef boost::shared_ptr FooPtr;
我试着这样使用:
std::vector<std::string> functionCalls;
FooPtr foo = boost::make_shared<Foo>(functionCalls);
std::向量函数调用;
FooPtr foo=boost::使_共享(函数调用);
我在VS20012中编译得很好,但在GCC4.3.4中编译不了
以下是编译器错误:
boost/boost_1_54_0/boost/smart_ptr/make_shared_object.hpp: In function 'typename boost::detail::sp_if_not_array<T>::type boost::make_shared(const A1&) [with T = Foo, A1 = std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >]':
main.cc:39: instantiated from here
boost/boost_1_54_0/boost/smart_ptr/make_shared_object.hpp:711: error: no matching function for call to 'Foo::Foo(const std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&)'
Foo.h:23: note: candidates are: Foo::Foo(std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&)
Foo.h:21: note: Foo::Foo(const Foo&)
boost/boost\u 1\u 54\u 0/boost/smart\u ptr/make\u shared\u object.hpp:在函数“typename boost::detail::sp\u if\u not\u array::type boost::make\u shared(const A1&)[带T=Foo,A1=std::vector]:
main.cc:39:从此处实例化
boost/boost_1_54_0/boost/smart_ptr/make_shared_object.hpp:711:错误:调用“Foo::Foo(const std::vector&)”时没有匹配函数
Foo.h:23:注:候选对象是:Foo::Foo(std::vector&)
Foo.h:21:note:Foo::Foo(constfoo&)
如果我将Foo的构造更改为常量,那么它可以编译。但是,我需要通过引用来传递。您认为这个问题是什么?如文档所述,如果没有C++11支持,make_shared只能通过const
引用获取其参数
有了C++11支持,它可以接受任何引用类型,并将它们转发给构造函数。想必这就是VS正在做的
如文档中所述,您可以通过将非常量引用包装在boost::ref
中来传递它:
FooPtr foo = boost::make_shared<Foo>(boost::ref(functionCalls));
fooptrfoo=boost::使_共享(boost::ref(functionCalls));
@juanchopanza:右值在哪里?@LightnessRacesinOrbit我显然误读了错误。我删除了以前令人困惑的评论。