我想编写一个模板,该模板可以将类型解构为具有非类型模板参数及其非类型模板参数的模板。例如,它将Array<5>解构为template<int> Array和5,但通常可用于任何类型的非类型模板参数(整数类型,指针,成员指针等)。
首先尝试使用模板特化:
template<typename T> struct foo { enum { n = 1 }; };
template<int x> struct bar { enum { n = x }; };
template<typename T, template<T> class X, T x>
struct foo< X<x> > { enum { n = x }; }; // here x must be of integral type, but that's just for testing
int main(int, char**) { return foo< bar<16> >::n; }
test145.cpp:6:8: warning: class template partial specialization contains a template parameter that can not be deduced; this partial specialization will never be used
struct foo< X<x> > { enum { n = x }; };
^~~~~~~~~~~
test145.cpp:5:19: note: non-deducible template parameter 'T'
template<typename T, template<T> class X, T x>
^
1 warning generated.
template<typename T, T x>
struct box
{
static constexpr T value() { return x; }
};
template<typename T, template<T> class X, T x>
box<T, x> foo(X<x>);
template<int> struct asdf { };
int main(int, char**) { return decltype(foo(*(asdf<9>*)0))::value(); }
test150.cpp:12:41: error: no matching function for call to 'foo'
int main(int, char**) { return decltype(foo(*(asdf<9>*)0))::value(); }
^~~
test150.cpp:8:11: note: candidate template ignored: couldn't infer template argument 'T'
box<T, x> foo(X<x>);
^
1 error generated.
template<T>,我也不认为您也可以编写template<T*>。)最佳答案
另一个问题是询问基本相同的问题,只是要求模板类型参数,而不是模板非类型参数:template metaprogramming: (trait for?) dissecting a specified template into types T<T2,T3 N,T4, ...>
对于类型参数,这确实很容易。代码如下:
#include <tuple>
#include <vector>
template <class T> struct explode;
template <template <class... Args> class T, class... N>
struct explode<T<N...>>
{
typedef T<N...> type;
template <class... Args> using template_ = T<Args...>;
template <int I> using type_parameter =
typename std::tuple_element<I, std::tuple<N...>>::type;
};
#if TESTING
void test_harness()
{
typedef explode<std::vector<int>> exv;
exv::template_<char> vchar; // The second parameter still has its default argument!
exv::template_<exv::type_parameter<0>, exv::type_parameter<1>> vint;
static_assert(std::is_same<exv::template_<char>, std::vector<char>>::value, "");
static_assert(std::is_same<decltype(vchar), std::vector<char>>::value, "");
static_assert(std::is_same<decltype(vint), std::vector<int>>::value, "");
static_assert(std::is_same<exv::type, std::vector<int>>::value, "");
static_assert(std::is_same<exv::type_parameter<0>, int>::value, "");
static_assert(std::is_same<exv::type_parameter<1>, std::allocator<int>>::value, "");
}
#endif
template <class... ArgTypes, template <ArgTypes... Args> class T, ArgTypes... N>
struct explode<T<N...>>
{
typedef T<N...> type;
template <ArgTypes... Args> using template_ = T<Args...>;
template <int I> using type_of_parameter =
typename std::tuple_element<I, std::tuple<ArgTypes...>>::type;
template <int I> struct nontype_parameter {
static constexpr type_of_parameter<I> value() {
return std::get<I>(std::tuple<ArgTypes...>(N...));
}
};
};
test.cc:8:8: warning: class template partial specialization contains a template
parameter that can not be deduced; this partial specialization will never
be used
struct explode<T<N...>>
^~~~~~~~~~~~~~~~
test.cc:7:20: note: non-deducible template parameter 'ArgTypes'
template <class... ArgTypes, template <ArgTypes... Args> class T, ArgTypes... N>
^
std::get版本替换constexpr,因为无论出于何种原因,标准库的std::get都不是constexpr。
关于c++ - 非类型模板参数的类型抽象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10759952/
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
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
我有一些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标准库是否已经带有这样一个类? 最佳