我认为 Boost::variant 在 1_54 中被破坏了。 我正在尝试将 std::unique_ptr 用作 boost 变体中的有界类型。
根据 1_54 文档,变体需要可复制构造或可移动构造。
http://www.boost.org/doc/libs/1_54_0/doc/html/variant/reference.html
所以我在我的代码中实现了移动构造函数并禁用了复制构造函数。
当我尝试将某些内容分配给变体对象时,它无法编译。 我尝试了各种不同的方法,包括使用 std::move 将数据分配给变体对象,但似乎没有任何效果。 根据编译错误堆栈跟踪,我确定问题出在 variant.hpp 中,它试图备份 rhs 数据。我想知道你们的想法,如果我认为 boost 变体文档是错误的,请告诉我。
提前致谢。
我正在使用 vs2010 和 C++11 进行编译。
这是我的测试代码:
#include <iostream>
#include <memory>
#include <utility>
#include <vector>
#include <string>
#pragma warning (push)
#pragma warning (disable: 4127 4244 4265 4503 4512 4640 6011)
#include <boost/optional.hpp>
#include <boost/variant.hpp>
#include <boost/ref.hpp>
#include <boost/shared_ptr.hpp>
#pragma warning (pop)
#include <boost/foreach.hpp>
#include <boost/format.hpp>
using boost::format;
using boost::str;
using namespace std;
class UniqueTest
{
};
class Foo
{
public:
std::unique_ptr<UniqueTest> testUniquePtr;
Foo() { std::cout << "Foo::Foo\n"; }
Foo (Foo&& moveData)
{
}
Foo& operator=(Foo&& moveData)
{
return *this;
}
private:
Foo(Foo& tt);
Foo& operator=(const Foo& tt);
};
int main()
{
Foo x = Foo();
boost::variant<std::wstring,Foo> m_result2;
std::wstring testString = L"asdf";
m_result2 = testString; //Fails
//m_result2 = std::move(testString); //Fails
//m_result2 = std::move(x); //Fails
boost::get<Foo>(m_result2).testUniquePtr.get ();
return 0;
}
最佳答案
Does my code as is compile for anyone?
不,它不会,变体将尝试调用缺少的复制构造函数。 (Foo::Foo(Foo const&) 甚至没有声明):
boost/variant/variant.hpp|756 col 9| error: no matching function for call to ‘Foo::Foo(const Foo&)’
即使您确实声明了它,它也不会起作用:
test.cpp|43 col 6| error: ‘Foo::Foo(const Foo&)’ is private
评论里有提到但是你需要
使复制构造函数成为复制构造函数(为了好的风格)
Foo(Foo const& tt) = delete;
Foo& operator=(const Foo& tt) = delete;
使移动构造函数/赋值noexcept,否则(如std::vector)variant将拒绝移动东西因为它不会异常安全。
Foo(Foo && moveData) noexcept { }
Foo& operator=(Foo && moveData) noexcept { return *this; }
这是一个在 GCC 和 Clang 上编译的简化示例:
#include <iostream>
#include <memory>
#include <string>
#include <boost/variant.hpp>
struct UniqueTest { };
struct Foo
{
public:
std::unique_ptr<UniqueTest> testUniquePtr;
Foo() { std::cout << "Foo::Foo\n"; }
Foo(Foo && moveData) noexcept { }
Foo& operator=(Foo && moveData) noexcept { return *this; }
Foo(Foo const& tt) = delete;
Foo& operator=(const Foo& tt) = delete;
};
int main()
{
Foo x = Foo();
boost::variant<std::wstring, Foo> m_result2;
std::wstring testString = L"asdf";
m_result2 = testString; //Fails
//m_result2 = std::move(testString); //Fails
//m_result2 = std::move(x); //Fails
boost::get<Foo>(m_result2).testUniquePtr.get();
}
关于c++ - 在 1_54 中破坏了 boost 变体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19718726/
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:
我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我
最近,我安装了OSXMavericks,它似乎弄乱了我的开发环境。我在运行“railsnewfirst_app”后收到此消息:Youruseraccountisn'tallowedtoinstalltothesystemRubygems.Youcancancelthisinstallationandrun:bundleinstall--pathvendor/bundletoinstallthegemsinto./vendor/bundle/,oryoucanenteryourpasswordandinstallthebundledgemstoRubygemsusingsudo.Pass
我有一个数组数组,想将元素附加到子数组。+=做我想做的,但我想了解为什么push不做。我期望的行为(并与+=一起工作):b=Array.new(3,[])b[0]+=["apple"]b[1]+=["orange"]b[2]+=["frog"]b=>[["苹果"],["橙子"],["Frog"]]通过推送,我将推送的元素附加到每个子数组(为什么?):a=Array.new(3,[])a[0].push("apple")a[1].push("orange")a[2].push("frog")a=>[[“苹果”、“橙子”、“Frog”]、[“苹果”、“橙子”、“Frog”]、[“苹果”、“
有没有办法让Ruby能够做这样的事情?classPlane@moved=0@x=0defx+=(v)#thisiserror@x+=v@moved+=1enddefto_s"moved#{@moved}times,currentxis#{@x}"endendplane=Plane.newplane.x+=5plane.x+=10putsplane.to_s#moved2times,currentxis15 最佳答案 您不能在Ruby中覆盖复合赋值运算符。任务在内部处理。您应该覆盖+,而不是+=。plane.a+=b与plane.a=
我在我的应用程序中使用spree2.0.0稳定版。在产品展示页面上,所有变体都显示为单选按钮。我只想在下拉列表中显示它们。对此有什么想法吗?谢谢。 最佳答案 注意:此解决方案实现Spree“模板替换方法”,尤其是当您在应用程序设计或使用自定义设计中进行大量设计更改时。看这里http://guides.spreecommerce.com/developer/view.html否则,如果您使用的是Spree商店的默认设计或较小的更改,请使用“破坏”方法。前往:app/views/spree/products/_cart.html.erb
升级到OSXYosemite后,我现有的pow.cx安装不起作用。升级到最新的pow.cx无效。通过事件监视器重新启动它也没有成功。 最佳答案 卸载(!)并重新安装解决了这个问题。curlget.pow.cx/uninstall.sh|shcurlget.pow.cx|sh 关于ruby-on-rails-OSXYosemite更新破坏了pow.cx,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/q
出于某种原因,heroku尝试要求dm-sqlite-adapter,即使它应该在这里使用Postgres。请注意,这发生在我打开任何URL时-而不是在gitpush本身期间。我构建了一个默认的Facebook应用程序。gem文件:source:gemcuttergem"foreman"gem"sinatra"gem"mogli"gem"json"gem"httparty"gem"thin"gem"data_mapper"gem"heroku"group:productiondogem"pg"gem"dm-postgres-adapter"endgroup:development,:t
我是Ruby和这个网站的新手。下面两个函数是不同的,一个在函数外修改变量,一个不修改。defm1(x)x我想确保我理解正确-当调用m1时,对str的引用被复制并传递给将其视为x的函数。运算符当调用m2时,对str的引用被复制并传递给将其视为x的函数。运算符+创建一个新字符串,赋值x=x+"4"只是将x重定向到新字符串,而原始str变量保持不变。对吧?谢谢 最佳答案 String#+::str+other_str→new_strConcatenation—ReturnsanewStringcontainingother_strconc