我正在尝试为模板类内部的模板类编写外部类模板运算符。
我希望下面的片段能解释我的意思。
enum MyEnum {};
template <MyEnum a>
class ClassWithTemplateClass {
public:
template <bool B>
class TemplateClass {
// ...
};
};
当我这样写运算符时:
template <MyEnum enumVal, bool B>
auto operator<<(ClassWithTemplateClass<enumVal>::TemplateClass<B> &a, int b) {
// ...
return a;
}
编译器返回错误:
错误:将“operator<>
你能告诉我这个运算符应该怎么写吗?
最佳答案
ClassWithTemplateClass<enumVal>::是 nested name specifier这又是一个 non-deduced context .自 enumVal是出现在范围解析运算符 :: 左侧的模板参数,编译器无法推断出它的值。
<<运算符可以定义为 (1) 类中的 friend TemplateClass :
enum MyEnum { X, Y, Z };
template <MyEnum E>
struct ClassWithTemplateClass
{
template <bool B>
struct TemplateClass
{
friend auto& operator<<(TemplateClass& a, int b)
{
return a;
}
};
};
哪里TemplateClass始终引用 ClassWithTemplateClass<?>::TemplateClass<?> 的特定实例
或 (2) 里面ClassWithTemplateClass :
enum MyEnum { X, Y, Z };
template <MyEnum E>
struct ClassWithTemplateClass
{
template <bool B>
struct TemplateClass
{
};
template <bool B>
friend auto& operator<<(TemplateClass<B>& a, int b)
{
return a;
}
};
或者,(3) 您可以为每个预定义的枚举值提供一个单独的运算符定义(尽管它可以有比定义为常量更多的值),这样只有 B需要推导:
enum MyEnum { X, Y, Z };
template <MyEnum E>
struct ClassWithTemplateClass
{
template <bool B>
struct TemplateClass
{
};
};
template <bool B>
auto& operator<<(ClassWithTemplateClass<X>::TemplateClass<B>& a, int b)
{
return a;
}
template <bool B>
auto& operator<<(ClassWithTemplateClass<Y>::TemplateClass<B>& a, int b)
{
return a;
}
template <bool B>
auto& operator<<(ClassWithTemplateClass<Z>::TemplateClass<B>& a, int b)
{
return a;
}
,或 (4) 将模板参数存储为 TemplateClass 的静态数据成员,并使用它们来使运算符 SFINAE 可用并检索它们的值:
enum MyEnum { X, Y, Z };
template <MyEnum E>
struct ClassWithTemplateClass
{
template <bool B>
struct TemplateClass
{
static constexpr MyEnum ClassWithTemplateClass_E = E;
static constexpr bool TemplateClass_B = B;
};
};
template <typename T
, MyEnum E = T::ClassWithTemplateClass_E
, bool B = T::TemplateClass_B>
auto& operator<<(T& a, int b)
{
return a;
}
作为另一种选择,(5) 您可以从 operator<< 发送调用到另一个函数,并显式指定其模板参数,以便签名完全符合您的要求,并且模板参数是已知的:
enum MyEnum { X, Y, Z };
template <MyEnum E>
struct ClassWithTemplateClass;
template <MyEnum E, bool B>
auto& print(typename ClassWithTemplateClass<E>::template TemplateClass<B>& a, int b);
template <MyEnum E>
struct ClassWithTemplateClass
{
template <bool B>
struct TemplateClass
{
friend auto& operator<<(TemplateClass& a, int b)
{
return print<E, B>(a, b);
}
};
};
template <MyEnum E, bool B>
auto& print(typename ClassWithTemplateClass<E>::template TemplateClass<B>& a, int b)
{
return a;
}
关于c++ - 模板类内部模板类的外部类运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34381519/
我的瘦服务器配置了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中使用的“触发器”。这是PragmaticProgrammersguidetoRuby中的一个示例:a=(11..20).collect{|i|(i%4==0)..(i%3==0)?i:nil}返回:[nil,12,nil,nil,nil,16,17,18,nil,20]还有:a=(11..20).collect{|i|(i%4==0)...(i%3==0)?i:nil}返回:[nil,12,13,14,15,16,17,18,nil,20] 最佳答案 触发器(又名f/f)是
我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow
我的问题的一个例子是体育游戏。一场体育比赛有两支球队,一支主队和一支客队。我的事件记录模型如下:classTeam"Team"has_one:away_team,:class_name=>"Team"end我希望能够通过游戏访问一个团队,例如:Game.find(1).home_team但我收到一个单元化常量错误:Game::team。谁能告诉我我做错了什么?谢谢, 最佳答案 如果Gamehas_one:team那么Rails假设您的teams表有一个game_id列。不过,您想要的是games表有一个team_id列,在这种情况下
如何将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.你能做的最好的事情是:
我正在使用Mandrill的RubyAPIGem并使用以下简单的测试模板:testastic按照Heroku指南中的示例,我有以下Ruby代码:require'mandrill'm=Mandrill::API.newrendered=m.templates.render'test-template',[{:header=>'someheadertext',:main_section=>'Themaincontentblock',:footer=>'asdf'}]mail(:to=>"JaysonLane",:subject=>"TestEmail")do|format|format.h
所以这可能有点令人困惑,但请耐心等待。简而言之,我想遍历具有特定键值的所有属性,然后如果值不为空,则将它们插入到模板中。这是我的代码:属性:#===DefaultfileConfigurations#default['elasticsearch']['default']['ES_USER']=''default['elasticsearch']['default']['ES_GROUP']=''default['elasticsearch']['default']['ES_HEAP_SIZE']=''default['elasticsearch']['default']['MAX_OP