任何人都可以帮助我解释为什么论证推导没有像我预期的那样工作吗?请查看我的代码注释以了解我的思路?
#include <iostream>
#include <type_traits>
#include <iomanip>
#include <string>
using namespace std;
template<class T>
void deduce1(T args, string arg){
cout << "template<class T> void deduce1(T args) " << " argument passed in was: " << arg << " deduced as: " << typeid(T).name() << endl;
cout << "Is const: " << boolalpha << is_const<T>::value << endl;
cout << "Is reference: " << boolalpha << is_reference<T>::value << endl;
cout << "Is pointer: " << boolalpha << is_pointer<T>::value << endl;
}
template<class T>
void deduce2(T& args,string arg){
cout << "template<class T> void deduce2(T args) " << " argument passed in was: " << arg << " deduced as: " << typeid(T).name() << endl;
cout << "Is const: " << boolalpha << is_const<T>::value << endl;
cout << "Is reference: " << boolalpha << is_reference<T>::value << endl;
cout << "Is pointer: " << boolalpha << is_pointer<T>::value << endl;
}
template<class T>
void deduce3(T&& args,string arg){
cout << "template<class T> void deduce3(T args) " << " argument passed in was: " << arg << " deduced as: " << typeid(T).name() << endl;
cout << "Is const: " << boolalpha << is_const<T>::value << endl;
cout << "Is reference: " << boolalpha << is_reference<T>::value << endl;
cout << "Is rvalue reference: " << boolalpha << is_rvalue_reference<T>::value << endl;
cout << "Is pointer: " << boolalpha << is_pointer<T>::value << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
int a = 1;
const int b = 5;
int c[] = {12};
int const d[] = {12};
int& e = a;
deduce1(a,"int a = 1");
deduce1(b,"const int b = 5");
deduce1(c,"int c[] = {12}");
deduce1(d,"int const d[] = {12}"); // would have thought is_const<T> would return true any comments?
deduce1(e,"int& e = a");
deduce1(5,"5");
deduce2(a,"int a = 1");
deduce2(b,"const int b = 5"); //would have though type would be deduced as int const comments?
deduce2(c,"int c[] = {12}"); // why is this not returning true as a reference?
deduce2(d,"int const d[] = {12}"); // would have thought is_const<T> would return true any comments
deduce2(e,"int& e = a");
deduce3(a,"int a = 1");
deduce3(b,"const int b = 5");
deduce3(c,"int c[] = {12}"); // why is this not returning true as a reference?
deduce3(d,"int const d[] = {12}"); // would have thought is_const<T> would return true any comments
deduce3(e,"int& e = a");
deduce3(string("Hello"),"string(\"Hello\")"); // why not rvalue reference
return 0;
}
最佳答案
我会依次处理您的每条评论。
deduce1 和 d:
template <class T> void deduce1(T args, string arg);
int const d[] = {12};
deduce1(d, "int const d[] = {12}"); // would have thought is_const<T> would return true
首先重要的是,在 C++ 中,您不能按值将数组作为参数传递。表示数组的表达式几乎会立即衰减为指向其第一个元素的指针。所以此处为 T 推导的类型将是 int const *。
那么,为什么这行不通呢?问题是指针 不是const。指针指向的 int 是 const。如果您想报告所指向类型的 constness,您需要在检查 constness 之前使用 remove_pointer。因此,您可以将 const 检查更改为:
cout << "Is const: " << boolalpha << is_const<typename remove_pointer<T>::type>::value << endl;
但是,它之前给出的结果是正确答案。指针确实不是 const。事实上,即使指针是 const,顶层 const 也会在类型推导完成之前被剥离,所以 T 不会是 const 无论如何。这样做的原因是,如果你按值传递,你真的不关心参数的 constness,因为你无论如何都要复制它。
deduce2 和 b:
template <class T> void deduce2(T& args,string arg);
const int b = 5;
deduce2(b,"const int b = 5"); //would have though type would be deduced as int const
不确定这里发生了什么,但我的输出是:
deduced as: i
Is const: true
Is reference: false
Is pointer: false
完全符合预期。
deduce2 与 c:
int c[] = {12};
deduce2(c,"int c[] = {12}"); // why is this not returning true as a reference?
这是说它不是引用,原因与所有 deduce2 调用都这么说一样(并且您在整个 deduce3 中也有这个问题)。在您的函数中,您正在检查 T 的类型,但 args 的类型是 T&。您只检查 & 之前的部分。所以在这里,args 的类型是 int (&)[1] c 但您只是检查 int [1] 是否是引用。通过在检查中使用 decltype(args) 而不是 T 来修复它。
deduce2 和 d:
int const d[] = {12};
deduce2(d,"int const d[] = {12}"); // would have thought is_const<T> would return true
与 deduce1 相同的问题与 d(第 1 项)。
deduce3 和 c:
template <class T> void deduce3(T&& args,string arg);
int c[] = {12};
deduce3(c,"int c[] = {12}"); // why is this not returning true as a reference?
与 deduce2 使用 c(第 3 项)的问题相同。
deduce3 和 d:
int const d[] = {12};
deduce3(d,"int const d[] = {12}"); // would have thought is_const<T> would return true
与 deduce1 相同的问题与 d(第 1 项)。
使用:string("Hello")推导3
deduce3(string("Hello"),"string(\"Hello\")"); // why not rvalue reference
我假设您对“通用引用”有所了解。斯科特迈耶斯有一个伟大的article和 talk关于这个问题。
在这种情况下,因为 string("Hello") 是一个右值表达式,类型 T 被推断为 string(由T&& 形式的推导右值引用类型的规则)。 args 的类型现在是 string &&。因此,如果您检查 decltype(args) 是否为右值,它会说 true。但是 T 本身不是。
如果您修复代码,使 T 上的所有类型特征现在都在 decltype(args) 上,您将遇到新问题。当 args 是引用类型时,您期望为 const 的某些类型将不会。那是因为没有 const 引用这样的东西。与在项目 1 中使用 remove_pointer 类似,您需要使用 remove_reference 获取基础类型并检查它是否为 const。
关于C++模板参数推导过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13265842/
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"
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择: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
我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use
对于作为String#tr参数的单引号字符串文字中反斜杠的转义状态,我觉得有些神秘。你能解释一下下面三个例子之间的对比吗?我特别不明白第二个。为了避免复杂化,我在这里使用了'd',在双引号中转义时不会改变含义("\d"="d")。'\\'.tr('\\','x')#=>"x"'\\'.tr('\\d','x')#=>"\\"'\\'.tr('\\\d','x')#=>"x" 最佳答案 在tr中转义tr的第一个参数非常类似于正则表达式中的括号字符分组。您可以在表达式的开头使用^来否定匹配(替换任何不匹配的内容)并使用例如a-f来匹配一