类似于this question , 我如何测试一个类 Impl公开继承自模板类 BaseTempl (即 class Impl : public BaseTempl< ... >{ ... }; ),而不指定模板参数?
但是,与上述问题不同的是,如果继承不是公开的,我希望测试仍然可以编译(并返回 false)。
理想情况下,代码允许我做这样的事情:
class alpha : public BaseTempl< int >{};
class bravo : BaseTempl< int >{};
class charlie{};
class delta : public BaseTempl< int >, public charlie {};
class echo : public delta {};
int main(){
publicly_inherits_from < alpha, BaseTempl > (); // true
publicly_inherits_from < bravo, BaseTempl > (); // false
publicly_inherits_from < charlie, BaseTempl > (); // false
publicly_inherits_from < delta, BaseTempl > (); // true
publicly_inherits_from < echo, BaseTempl > (); // true
}
当我尝试编译上述代码时,链接问题的答案给出了以下错误:
error: ‘BaseTempl<int>’ is an inaccessible base of ‘bravo’
最佳答案
以下基于 SFINAE 的方法似乎可以产生预期的结果。作为额外的好处,BaseTempl 模板可以采用可变参数,而不仅仅是一个参数:
#include <iostream>
template<typename ...Args> class BaseTempl {};
template<typename T> class inherits_from_basetempl {
public:
template<typename ...Args>
static const bool sfinae_param(const BaseTempl<Args...> &a);
template<typename V=T>
static constexpr auto is_inherits(int)
-> decltype(sfinae_param(std::declval<V &>()))
{
return true;
}
static constexpr bool is_inherits(...)
{
return false;
}
static const bool value=is_inherits(0);
};
class alpha : public BaseTempl< int >{};
class bravo : BaseTempl< int >{};
class charlie{};
class delta : public BaseTempl< int >, public charlie {};
class echo : public delta {};
int main()
{
std::cout << inherits_from_basetempl<alpha>::value << std::endl;
std::cout << inherits_from_basetempl<bravo>::value << std::endl;
std::cout << inherits_from_basetempl<charlie>::value << std::endl;
std::cout << inherits_from_basetempl<delta>::value << std::endl;
std::cout << inherits_from_basetempl<echo>::value << std::endl;
}
结果:
$ gcc --version
gcc (GCC) 5.3.1 20151207 (Red Hat 5.3.1-2)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ -std=c++14 -o t t.C 2>&1 | less
$ ./t
1
0
0
1
1
关于c++ - 检查 C++ 类是否公开继承自带有匿名参数的模板类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35714153/
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
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
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
我正在为一个项目制作一个简单的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)