从枚举器获取枚举类型
Get enumeration type from enumerator
假设我有这样的代码:
#include <cstdio>
enum Foo { Foo_A, Foo_B };
enum Bar { Bar_A, Bar_B };
template<typename enumeration, int enumerator>
struct tpl;
template<int enumerator>
struct tpl<Foo, enumerator>
{
tpl() { printf("Foo: %dn", enumerator); }
};
template<int enumerator>
struct tpl<Bar, enumerator>
{
tpl() { printf("Bar: %dn", enumerator); }
};
int main()
{
tpl<Foo, Foo_A> foo_a;
tpl<Foo, Foo_B> foo_b;
tpl<Bar, Bar_A> bar_a;
tpl<Bar, Bar_B> bar_b;
return 0;
};
有没有办法减少使用站点的"重复"?也就是说,我不能从枚举器"Foo_A"等中推导出枚举类型"Foo",并以某种方式在上面的模板代码中使用它吗?枚举类在这里有帮助吗?
您的问题的答案是,没有目前没有办法做到这一点。你所面对的是众所周知的template <typename T, T t>
习语。事实上,如果你用谷歌搜索,你会发现有近75000次点击,而且没有解决办法。你必须尽你所能做到专业化。
但好消息就在眼前。在过去的十年里,这已经多次向标准委员会提出:
- N3405提出了
template <typename T t>
的容差。其中T
是类型,t
是值,只有一个值作为模板参数传递 - N3601提出
template <typename T, T t>
是一种特殊情况,编译器只接受单个值作为模板参数,并由此推断出T
和t
- N4469建议允许关键字
auto
指定一个元参数:template <auto t>
,以便将一个值作为模板参数t
传递,并推导出其类型
2015年5月4日,在Lenexa举行的会议上,N4469终于获得了支持,并在标准委员会会议上提出了修订要求:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4539.html#9
第一次修订P0127R0于2015年9月25日提交。
2016年3月4日提交了后续修订:P0127R1,建议对概念工作草案部分进行编辑。
P0127R2专注于充分说明工作草案中非概念部分的习语变化,因为尚不清楚概念是否会包含在C++17中。该修订于2016年6月23日被接受为C++17标准:http://developerblog.redhat.com/2016/07/13/red-hat-iso-c-standards-meeting-june-2016-core-language/
因此,随着C++17的到来,您将能够处理template <typename T, T t>
习惯用法并使用:
template <auto t>
struct tpl{
tpl(){ cout << typeid(decltype(t)).name() << ": " << t << endl; }
};
相关文章:
- 强枚举类型定义:Clang Bug 还是 C++11 标准不确定性?
- 如何注册枚举类型到QJSEngine,以便在脚本环境中使用?
- 通过包装器从 C 访问C++ API 时,如何访问枚举类型
- C++/错误:表达式必须具有整数或无作用域枚举类型
- 如何理解c++中在命名空间内部定义的枚举类型enum
- 在类访问混淆中声明的枚举类型
- 在枚举类型上使用std::max是不是一种糟糕的做法
- C++ 在方程中使用变量;错误:表达式必须具有整数或无作用域枚举类型及其他
- 是否可以使用泛型枚举类型作为函数的参数?
- Visual Studio 2017 生成工具与枚举类型的问题
- 使用枚举类型管理存储顺序
- 如何检查类中定义的枚举类型变量是否被分配给?
- 如何从枚举类型中获取枚举值?
- 为什么C++不为枚举类型提供默认"operator>>"函数?
- 确保枚举类型的键/值在软件Revs之间永远不会更改
- MSVC 1 位枚举类型等于 -1,并且相等性测试失败
- 我们可以专业化类模板的枚举(类型)成员吗?
- 错误:以增量方式填充向量时,表达式必须具有整数或无作用域枚举类型
- 枚举类型的重载>>运算符(工作日)
- C 中的枚举类型字符串类别