我有以下内容
#include <iostream>
#include <memory>
template<typename _type>
class handle
{
using ptr = std::shared_ptr<_type>;
using pptr = std::shared_ptr<ptr>;
public:
handle(handle<_type> const & other) :
mData(make_pptr(*(other.mData)))
{}
handle(_type && data) :
mData(make_pptr(std::move(data)))
{}
private:
pptr mData;
template<typename ..._args>
constexpr auto make_ptr(_args && ...args)
{
return std::make_shared<_type>(std::forward<_args>(args)...);
}
constexpr auto make_pptr(ptr const & pointer)
{
return std::make_shared<ptr>(pointer);
}
template<typename ..._args>
constexpr auto make_pptr(_args && ...args)
{
return std::make_shared<ptr>(make_ptr(std::forward<_args>(args)...));
}
};
int main()
{
handle<int> h = 5;
handle<int> h2(h);
}
用g++-4.9 --std=c++14 -O0 -o main main.cpp编译
handle<int> h2(h);
不编译。问题函数都是
的重载make_pptr
据我了解,将始终选择模板函数,因为编译器会尝试找到最 专用的函数调用,而完美的转发恰恰会创建它。
我发现以下两个页面似乎使用类型特征 std::enable_if 和 std::is_same 来处理这个问题。
https://akrzemi1.wordpress.com/2013/10/10/too-perfect-forwarding/
http://www.codesynthesis.com/~boris/blog/2012/05/30/perfect-forwarding-and-overload-resolution/
真正的问题是,我如何更改这个函数,以便在我向工厂函数传递一个已经存在的指针时调用非模板函数?
有通用的方法吗?
最佳答案
作为 Jarod 的 answer解释,在构造函数中
handle(handle<_type> const & other) :
mData(make_pptr(*(other.mData)))
{}
你调用make_pptr参数类型为 shared_ptr<_type>& ,这使得 make_pptr 的完美转发过载比使用 shared_ptr<_type> const& 的匹配更好.您可以将参数转换为 const&如他所示,或者您可以添加另一个重载 make_pptr这需要一个非 const左值引用。
constexpr auto make_pptr(ptr & pointer)
{
return std::make_shared<ptr>(pointer);
}
另一种选择是限制完美转发过载,使其仅在参数包的第一个参数不是 shared_ptr<_type> 时才可行。 .
一些帮助程序评估参数包中的第一个类型是否是 shared_ptr<T>
namespace detail
{
template<typename... _args>
using zeroth_type = typename std::tuple_element<0, std::tuple<_args...>>::type;
template<typename T, bool eval_args, typename... _args>
struct is_shared_ptr
: std::false_type
{};
template<typename T, typename... _args>
struct is_shared_ptr<T, true, _args...>
: std::is_same<std::decay_t<zeroth_type<_args...>>,
std::shared_ptr<T>
>
{};
}
然后约束完美转发make_pptr如下
template<typename ..._args,
typename = std::enable_if_t<
not detail::is_shared_ptr<_type, sizeof...(_args), _args...>::value
>
>
constexpr auto make_pptr(_args && ...args)
{
return std::make_shared<ptr>(make_ptr(std::forward<_args>(args)...));
}
我还必须更改您的 make_ptr重载,因为您在示例中定义它的方式需要 _type可从 nullptr 构建.
constexpr auto make_ptr()
{
return std::make_shared<_type>();
// no nullptr arg above, shared_ptr default ctor will initialize _type* to nullptr
}
关于c++ - 我想完善除特定类型之外的前向可变参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30464342/
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option
我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano
有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s