- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
以下使用 VS2015 编译,但在 VS2017 中失败并出现以下错误。代码是否在做一些非标准的事情,已在 VS2017 中修复,或者 VS2017 应该编译它?
#include "stdafx.h"
#include <type_traits>
template <typename E>
constexpr auto ToUnderlying(E e)
{
return static_cast<std::underlying_type_t<E>>(e);
}
template<typename T>
bool constexpr IsFlags(T) { return false; }
template<typename E>
std::enable_if_t<IsFlags(E{}), std::underlying_type_t<E>> operator | (E lhs, E rhs)
{
return ToUnderlying(lhs) | ToUnderlying(rhs);
}
enum class PlantFlags { green = 1, edible = 2, aromatic = 4, frostTolerant = 8, thirsty = 16, growsInSand = 32 };
bool constexpr IsFlags(PlantFlags) { return true; }
int main()
{
auto ored = PlantFlags::green | PlantFlags::frostTolerant;
return 0;
}
错误是:
c:\main.cpp(24): error C2893: Failed to specialize function template 'enable_if<false,_Ty>::type
operator |(E,E)'
with
[
_Ty=underlying_type<_Ty>::type
]
c:\main.cpp(24): note: With the following template arguments:
c:\main.cpp(24): note: 'E=PlantFlags'
c:\main.cpp(24): error C2676: binary '|': 'PlantFlags' does not define this operator or a conversion to a type acceptable to the predefined operator
最佳答案
这绝对是 Visual Studio 中的错误。它compiles on GCC and Clang .它似乎与评估为模板参数的 constexpr
函数有关。作为临时解决方法,您可以制作一个模板变量:
template <typename T>
bool constexpr is_flags_v = IsFlags(T{});
template<typename E>
std::enable_if_t<is_flags_v<E>, std::underlying_type_t<E>> operator | (E lhs, E rhs)
{
return ToUnderlying(lhs) | ToUnderlying(rhs);
}
关于c++ - 为什么这个enable_if函数模板不能专用于VS2017?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46104002/
我正在使用这样的模板别名: template ::value>::type> using vec2 = std::pair; template ::value>::type> using vec3 =
基本问题陈述 我正在学习 SFINAE。我尝试了一个非常简单的 enable_if : // 1: A foo() that accepts arguments that are derived fr
在language reference of std::enable_if at cppreference包括以下注释 Notes A common mistake is to declare two
我对 SFINAE 有基本的了解,我想我理解了很多关于如何 std::enable_if 的例子。利用它来选择函数模板特化,但我很难理解它如何用于类模板。 以下例子来自cppreference.com
这有什么问题? 我认为这应该在使用 enable if 时起作用??? 帮助?? 不应该排除第二个构造函数吗? #include #include #include template class
我试图在模板类中声明函数,以便函数声明依赖于模板类型参数。 template struct Block { static bool parse(int32_t index,
我正在尝试一种基于类模板参数来专门化成员函数的方法,而不必在类上使用 SFINAE(并导致代码重复或创建另一个类)。 由于两个模板参数不能是可选的,并且参数 enable_if 在指南中是不受欢迎的,
我遇到一个问题,未知代码正在使用试图在编译时和运行时取消引用类型的元模板。这意味着,它们遍历指针层次结构直到找到匹配器。 现在通常这很好用。但是当用户传递类似的东西时: typedef struct
我试图了解 std::enable_if 是如何工作的 inn 模板参数。 #include #include #include using namespace std; class Inter
有谁知道为什么下面的代码可以编译 static const size_t CONSTANT = /* ... */; template = 0 > res_type foo() { // ...
我有一个类定义为: template class AdjacencyList; 其中 V 和 E 分别是顶点和边值的类型。 我目前正在尝试在 AdjacencyList 中定义以下成员函数: std
我有以下代码无法在 VC2010 上编译: #include using namespace std; template typename enable_if::type foo() { retu
为什么编译器对 std::tuple 的访问者具有以下定义顺序很重要 namespace TupleVisit{ //This function SHOULD BE DEFINED S
我想使用 type_traits 是否通过 shared_ptr 重载。 struct A { A(int i) : x(i) {} int x; }; int main() {
作为一个实验,我试图使一个没有参数的 void 成员函数根据类模板参数改变行为: #include #include template class MyClass { public: void
我正在使用 enable_if 语句来删除考虑中的可能方法。 #include "gmpxx.h" #include template struct is_ring_field { }; temp
是否有任何“更干净”的方式(我的意思是减少重复代码)来编写以下内容? template class Test { struct Foo1 { int a; }; struct F
在C ++中,请考虑以下示例: template struct q; template struct q { q() { cout struct q {
我对 std::enable_if 很陌生,想知道如何使用它。我有一个模板类: template class foo { } 现在我只想在 a + b 等于 10 时实例化它。我可以使用 std::e
我最近遇到了一个有趣的 enable_if 用法版本,它用于有条件地启用具有更好可读性的函数,因为该函数的返回类型不是 enable_if 的一部分(请参阅 cleaner_usage): #incl
我是一名优秀的程序员,十分优秀!