我需要编写一个元编程结构,当给定一个枚举类型时,它返回该枚举的基础类型,但当给定一个整数时,它返回该整数。
例如:
enum Enum : short { VALUE1, VALUE2 };
int_type<long>::type // -> long
int_type<Enum>::type // -> short
我试过了
template< typename Type >
struct int_type {
using type = typename std::enable_if< std::is_enum<Type>::value, typename std::underlying_type<Type>::type >::type;
};
template< typename Type >
struct int_type {
using type = typename std::enable_if< std::is_integral<Type>::value, Type >::type;
};
但它提示结构的重新定义。
我也试过了,但是
template< typename Type >
struct int_type {
using type = typename std::enable_if< std::is_enum<Type>::value, typename std::underlying_type<Type>::type >::type;
using type = typename std::enable_if< std::is_integral<Type>::value, Type >::type;
};
但随后它提示成员 type 的重新定义。
我的元编程技能到此为止,有人可以帮忙吗?
编辑:我还应该提到,我们的项目仅限于 C++11。
最佳答案
您尝试的问题是您将同一事物定义了两次,尽管它的部分使用了 enable_if,但当 enable_if 不正确时,它不会禁用整个封闭定义.即,您有两个 int_type::type 定义,即使这些定义中的一个(或两个)无效。
你要找的是 std::conditional :
template< typename Type >
struct int_type {
using when_enum
= std::underlying_type<Type>;
using when_integral
= std::enable_if<std::is_integral<Type>::value, Type>;
using type
= typename std::conditional< std::is_enum<Type>::value,
when_enum, when_integral
>::type::type;
};
或者在 C++14 及更高版本中:
template< typename Type >
struct int_type {
using when_enum
= std::underlying_type<Type>;
using when_integral
= std::enable_if<std::is_integral<Type>::value, Type>;
using type
= typename std::conditional_t< std::is_enum<Type>::value,
when_enum, when_integral
>::type;
};
但我想我可能会把它写成一个别名模板:
template< typename Type >
using int_type = std::conditional_t<
std::is_enum<Type>::value,
std::underlying_type<Type>,
std::enable_if<std::is_integral<Type>::value, Type>>;
也许可以进一步简化它,让 int_type<T> 成为实际类型的别名,这样你就不需要说 typename int_type<T>::type 了:
template< typename Type >
using int_type = typename std::conditional_t<
std::is_enum<Type>::value,
std::underlying_type<Type>,
std::enable_if<std::is_integral<Type>::value, Type>>::type;
(注意,在 C++17 中,您可以使用 std::is_enum_v<Type> 和 std::is_integral_v 而不是 is_xxx<Type>::value 。)
关于c++ - 返回枚举的基础类型和整数的整数的元编程构造,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56972288/
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案
有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳
几个月前,我读了一篇关于rubygem的博客文章,它可以通过阅读代码本身来确定编程语言。对于我的生活,我不记得博客或gem的名称。谷歌搜索“ruby编程语言猜测”及其变体也无济于事。有人碰巧知道相关gem的名称吗? 最佳答案 是这个吗:http://github.com/chrislo/sourceclassifier/tree/master 关于ruby-寻找通过阅读代码确定编程语言的rubygem?,我们在StackOverflow上找到一个类似的问题:
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s
我正在玩HTML5视频并且在ERB中有以下片段:mp4视频从在我的开发环境中运行的服务器很好地流式传输到chrome。然而firefox显示带有海报图像的视频播放器,但带有一个大X。问题似乎是mongrel不确定ogv扩展的mime类型,并且只返回text/plain,如curl所示:$curl-Ihttp://0.0.0.0:3000/pr6.ogvHTTP/1.1200OKConnection:closeDate:Mon,19Apr201012:33:50GMTLast-Modified:Sun,18Apr201012:46:07GMTContent-Type:text/plain
所以我开始关注ruby,很多东西看起来不错,但我对隐式return语句很反感。我理解默认情况下让所有内容返回self或nil但不是语句的最后一个值。对我来说,它看起来非常脆弱(尤其是)如果你正在使用一个不打算返回某些东西的方法(尤其是一个改变状态/破坏性方法的函数!),其他人可能最终依赖于一个返回对方法的目的并不重要,并且有很大的改变机会。隐式返回有什么意义?有没有办法让事情变得更简单?总是有返回以防止隐含返回被认为是好的做法吗?我是不是太担心这个了?附言当人们想要从方法中返回特定的东西时,他们是否经常使用隐式返回,这不是让你组中的其他人更容易破坏彼此的代码吗?当然,记录一切并给出