我的目标是实现一个检测嵌套 using 是否存在的谓词别名(或 typedef )充当轻量级标签以指示类具有某些属性(用于泛型编程)。例如,has_my_tag<T>谓词的行为应如下所示:
struct A {
using my_tag = void;
};
struct B {};
int main()
{
static_assert(has_my_tag<A>::value, ""); // evaluate to true if my_tag=void is present
static_assert(!has_my_tag<B>::value, ""); // false otherwise
}
用户@JoelFalcou 称其为“轻量级类型分类成语”并在 this answer 中提供了解决方案.我找不到任何关于那个名字的成语的引用资料(你知道吗?)这是乔尔对 has_my_tag<> 的实现。 :
template<class T, class R = void>
struct enable_if_type { typedef R type; };
template<class T, class Enable = void>
struct has_my_tag : std::false_type {};
template<class T>
struct has_my_tag<T, typename enable_if_type<typename T::my_tag>::type> :
std::true_type
{};
这里是编译器资源管理器上的工作版本:https://godbolt.org/z/EEOBb-
我想出了以下简化版本:
template<class T, class Enable = void>
struct has_my_tag : std::false_type {};
template<class T>
struct has_my_tag<T, typename T::my_tag> : std::true_type
{};
我的问题:简化版本是一种可以接受的实现习语的方式吗?在某些情况下它会失败吗?是否有适用于 C++11 的更简单版本?我应该更喜欢哪个版本?
据我了解,Joel 的版本将允许 my_tag为任何类型起别名,而我的版本需要 my_tag别名 void .但考虑到为轻量级谓词测试标记类型的目标,我不清楚哪个版本是首选。
辅助问题:还有,这个成语还有别的名字吗?它是否在我可以调查的任何图书馆中使用?到目前为止,我还没有找到可以显示任何搜索结果的名称。
最佳答案
对于您的设置,原始版本和您的没有区别。两者都使用 SFINAE 选择正确的 has_my_tag。但是,您的版本确实限制了您的 typedef/using成为my_tag=void .如果 my_tag 与任何其他类型一样是 typedef,则您的特化将不匹配,并且您最终将实例化主模板,如图所示 here .
这样做的原因是您在 main 中实例化模板的位置,static_assert(has_my_tag<A>::value, "");您没有指定第二个参数,因此使用默认值 (void),即 has_my_tag<A,void>::value
您的专业必须与此相匹配才能被考虑。
enable_if_type的用法,(基本上是在 c++17 中完成 void_t 的工作)是在 ::type 上启用 SFINAE T 的成员,但总是导致无效,这样您的特化将在 ::type 时匹配存在,无论 my_tag 的类型如何类型定义。
这让您只需担心它是否存在,而不是它的类型;
我个人会使用不依赖于 my_type 的方法被 typedef 定义为 void,要么是 enable_if_type 版本,要么是类似...
#include <iostream>
#include <type_traits>
struct A {
using my_tag = void;
};
struct B {};
struct C {
using my_tag = int; // with void_t also works with my_tag = int
};
struct D {
struct my_tag{}; //or some struct as the tag
};
// same as your enable_if_type
template <typename...>
using void_t = void;
template<class T, class Enable = void>
struct has_my_tag : std::false_type {};
template<class T>
struct has_my_tag<T, void_t<typename T::my_tag>> : std::true_type
{};
int main() {
std::cout << has_my_tag<A>::value << std::endl;
std::cout << has_my_tag<B>::value << std::endl;
std::cout << has_my_tag<C>::value << std::endl;
std::cout << has_my_tag<D>::value << std::endl;
return 0;
}
关于c++ - "lightweight type categorization idiom"的最简单实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52777854/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub