最近我正在研究模板类中名称的著名“两阶段名称查找”的确切含义。虽然我已经阅读了很多这方面的文章,但我仍然无法对此一无所知。现在我对下面显示的代码感到困惑:
template<typename T>
class A
{
public:
void f(T, T){};
};
namespace ns
{
typedef int TT;
void f(int, int){};
};
template<typename T>
class B : public A<T>
{
public:
void g()
{
//f(T(), T()); // it's fine for error here
typedef ns::TT TTT;
f(TTT(), T()); // why this issued an error?
f(ns::TT(), T()); // and this?
}
};
/* I also think it's OK to move ns here */
// namespace ns
// {
// typedef int TT;
// void f(int, int){};
//};
int main()
{
B<int> b;
b.g();
}
请注意第二条评论。由于“f”是一个从属名称,它的查找应该延迟到“main”函数中的实例化。那时,编译器应该在 main 函数的范围内执行参数依赖名称查找。我想现在它应该发现命名空间 ns 中的函数,但它仍然发出编译错误:
1.cpp: In instantiation of 'void B<T>::g() [with T = int]':
1.cpp:30:6: required from here
1.cpp:23:15: error: 'f' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive] f(TTT(), T()); //why this issued an error?
^
1.cpp:23:15: note: declarations in dependent base 'A<int>' are not found by unqualified lookup
1.cpp:23:15: note: use 'this->f' instead
有人能给我解释一下吗?谢谢。
最佳答案
依赖于参数的查找仅搜索参数类型的关联类和命名空间。 typedef 只是一个透明的别名,类似于 using 声明。
来自标准 n3485 草案,[basic.lookup.argdep]/2 关于参数依赖查找:
For each argument type
Tin the function call, there is a set of zero or more associated namespaces and a set of zero or more associated classes to be considered. The sets of namespaces and classes is determined entirely by the types of the function arguments (and the namespace of any template template argument). Typedef names and using-declarations used to specify the types do not contribute to this set.
[强调我的]
template<typename T>
class A
{
public:
void f(T, T){};
};
namespace ns
{
typedef int TT;
void f(int, int){};
};
template<typename T>
class B : public A<T> // note the base class is dependent
{
public:
void g()
{
//f(T(), T()); // it's fine for error here
typedef ns::TT TTT;
f(TTT(), T()); // why this issued an error?
f(ns::TT(), T()); // and this?
}
};
由于基类是依赖的,非限定查找时不会查找。因此,f可以使用依赖于参数的查找找到。你正确地说 f将仅在“第二阶段”(在实例化时)搜索,就像在您的调用中一样,至少有一个参数依赖于模板参数。
然而,ns 不是从属名称,以及TT (在 ns::TT 中)。因此,命名空间和 TT必须在它们用于 g 的定义之前声明.
是否写f(ns::TT(), T())或 f(T(), T())不影响 f 的一般规则在依赖于参数的查找期间搜索:仅关联的 namespace 和参数类型的类(T 和 ns::TT )。两者都是 int s B<int>::g() ,因此没有关联的类和命名空间。
f(TTT(), TTT())在 f 范围内更改查找现在是在名字查找阶段查找(它不是从属名称)。
这是一个参数依赖查找的例子:
namespace ns
{
struct TT {};
void f(TT, TT) {}
}
int main()
{
ns::TT x;
f(x, x);
}
现在,您也可以在类模板的成员函数中执行此操作:
namespace ns
{
struct TT {};
void f(TT, TT) {}
}
template<typename T>
struct B
{
void g()
{
f(T(), T());
}
};
int main()
{
B<ns::TT> x;
x.g();
}
但是,正如我所说,依赖于参数的名称查找不适用于基本类型,例如 int .
在上面的例子中,f再次依赖,因为至少有一个参数依赖于模板参数。因此,您也可以这样写:
template<typename T>
struct B
{
void g()
{
f(T(), T()); // looked up during the second phase,
// from the point of instantiation
}
};
namespace ns
{
struct TT {};
void f(TT, TT) {}
}
int main()
{
B<ns::TT> x;
x.g();
}
关于c++ - 在模板中执行参数相关名称查找时的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20924599/
我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass
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)
我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul
两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden