有什么方法可以在编译时检查字符串用户定义的文字吗
Is there any way for compile-time check of string user-defined literal?
我正在编写一个用户定义的string
文字,用于将月份名称转换为数字。这个文字的预期用法有点像
"Nov"_m
其应当返回CCD_ 2。
目前我的代码看起来像
constexpr Duration operator ""_m(const char* str, size_t len)
{
return convert_month_to_int(str, len);
}
其中constexpr int convert_month_to_int(const char, size_t)
是进行实际转换的函数(或者如果月份名称不正确则返回-1
(。
问题是,如果传递给这个文本的字符串没有命名任何月份,我想显示某种编译错误。我尝试以以下方式使用static_assert
:
constexpr Duration operator ""_m(const char* str, size_t len)
{
static_assert(convert_month_to_int(str, len) > 0, "Error");
return convert_month_to_int(str, len);
}
但这不起作用,因为编译器不确定CCD_。
有什么方法可以实现这种行为吗?
我以不同的方式处理了这个问题,既不使用枚举也不使用字符串文字,即使没有构造为constexpr
:,也会检测到错误的月份名称
#include "date.h"
int
main()
{
using namespace date::literals;
auto m1 = nov; // ok
static_assert(unsigned{nov} == 11, ""); // ok
auto m2 = not_a_month;
test.cpp:86:15: error: use of undeclared identifier 'not_a_month'
auto m2 = not_a_month;
^
1 error generated.
}
我使用的方法是定义一个类类型month
,它被记录为文本类类型。
然后我创建每个月的constexpr
实例:
CONSTDATA date::month jan{1};
CONSTDATA date::month feb{2};
CONSTDATA date::month mar{3};
CONSTDATA date::month apr{4};
CONSTDATA date::month may{5};
CONSTDATA date::month jun{6};
CONSTDATA date::month jul{7};
CONSTDATA date::month aug{8};
CONSTDATA date::month sep{9};
CONSTDATA date::month oct{10};
CONSTDATA date::month nov{11};
CONSTDATA date::month dec{12};
(CONSTDATA
是一个宏,用于帮助C++11 constexpr
不太支持的编译器(
我在一周中的几天里也使用了同样的技巧。
以上都是用clang和-std=c++11
编译的。它还将与gcc合作。VS中的constexpr
位已损坏,但其他一切都正常,包括在编译时检测错误的月份名称。
我同意使用枚举的建议。
但无论如何,在constexpr
函数中发出这样错误信号的常用方法是抛出异常。
constexpr Duration operator ""_m(const char* str, size_t len)
{
return convert_month_to_int(str, len) > 0 ? convert_month_to_int(str, len) : throw "Error";
}
例如,另请参见此问题。
相关文章:
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 使用用户定义函数的字符串反转
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- 用户输入字符串的文件附加问题..C++
- 从用户定义的类生成格式字符串?
- 我如何使它,无论用户用空白字符串按 Enter 多少次,它总是打印"开始"字符串?
- C++ - 使用用户输入的字符串数据检查结构字符串数据(无限执行 while 循环)
- 如何计算字符串中用户输入元素的出现次数?
- 是否可以创建一个用户定义的文本,将字符串文本转换为 own 类型的数组?
- 如何使用用户输入创建字符串队列?
- 正在尝试将用户输入传递到字符串(C++)中
- 将用户输入从 editLine 记录到字符串数组
- 是否可以用类似C/C++(或任何语言)的语言,从作为用户输入的字符串或文件中创建用户定义的数据类型
- 如何删除用户输入字符串末尾的空字符?
- C++代码,用于在用户插入的字符串的每个元音之后插入一对字符
- 用户输入字符串的结构的搜索向量
- 有没有办法通过作为用户输入的字符串来创建类实例名称?
- 在读取数据文件时,如何找到用户字符串输入的字谜?
- 如何将GUI用户字符串输入转换为使用C++库函数的格式
- 如何将用户字符串输入存储在数组中,C++