用户定义的MPL序列的文字:这合法吗
User-defined literal to MPL sequence: is this legal?
能够将传递给文字运算符的字符串转换为MPL序列将非常有用,因为这样我们就能够根据字符串的内容控制代码生成。以前,我认为这是不可能的,因为constexpr函数的参数在函数体中不被视为常量表达式。然而,我提出了以下变通方法,在Clang 3.4.2和GCC 4.8.2下编译:
#include <cstdint>
#include <iostream>
#include <typeinfo>
struct string
{
const uintmax_t m_str[64];
const size_t m_length;
template <class... Ts>
constexpr string(const Ts... ts) :
m_str{(uintmax_t)ts...}, m_length{sizeof...(Ts)} {}
constexpr size_t size() const { return m_length; }
constexpr size_t length() const { return m_length; }
constexpr uintmax_t operator[](size_t n) const { return m_str[n]; }
};
template <uintmax_t... Ts> struct sequence {};
constexpr auto
operator"" _tag(const char* str, size_t n)
{
return n == 0 ? string{} :
n == 1 ? string{str[0]} :
n == 2 ? string{str[0], str[1]} :
n == 3 ? string{str[0], str[1], str[2]} :
n == 4 ? string{str[0], str[1], str[2], str[3]} :
n == 5 ? string{str[0], str[1], str[2], str[3], str[4]} :
string{str[0], str[1], str[2], str[3], str[4], str[5]};
}
int main()
{
static constexpr auto string = "Hello!"_tag;
using sequence = sequence<string[0], string[1], string[2], string[3], string[4], string[5]>;
std::cout << typeid(sequence{}).name() << std::endl;
}
输出:
sequence<72ul, 101ul, 108ul, 108ul, 111ul, 33ul>
问题
这段代码是调用未定义的行为,还是合法的?
这是正确的。除非必要,否则条件运算符不会计算操作数。
不过,你做得太复杂了。尝试使用字符串文字运算符模板(Clang和GCC都支持该模板)。它们最初是为C++1Y计划的,但提案被拒绝了(不知道为什么,它是由Richard Smith完成的,这个功能是必不可少的)。
如果这还不够可移植(因为它现在只是一个编译器扩展),可以使用二进制递归宏和rtrim
类模板,比如VTMPL:
# define VTMPL_SPLIT_1(s, x) ( x < sizeof(s) ? s[x] : decltype(*s)() )
# define VTMPL_SPLIT_4(s, x) VTMPL_SPLIT_1 (s, x), VTMPL_SPLIT_1 (s, x+1) , VTMPL_SPLIT_1 (s, x+2) , VTMPL_SPLIT_1 (s, x+3)
# define VTMPL_SPLIT_16(s, x) VTMPL_SPLIT_4 (s, x), VTMPL_SPLIT_4 (s, x+4) , VTMPL_SPLIT_4 (s, x+8) , VTMPL_SPLIT_4 (s, x+12)
# define VTMPL_SPLIT_64(s, x) VTMPL_SPLIT_16 (s, x), VTMPL_SPLIT_16 (s, x+16) , VTMPL_SPLIT_16 (s, x+32) , VTMPL_SPLIT_16 (s, x+48)
# define VTMPL_SPLIT_256(s, x) VTMPL_SPLIT_64 (s, x), VTMPL_SPLIT_64 (s, x+64) , VTMPL_SPLIT_64 (s, x+128), VTMPL_SPLIT_64 (s, x+194)
# define VTMPL_SPLIT_1024(s, x) VTMPL_SPLIT_256(s, x), VTMPL_SPLIT_256(s, x+256), VTMPL_SPLIT_256(s, x+512), VTMPL_SPLIT_256(s, x+768)
# define VTMPL_STRING_IMPL(str, n) vtmpl::rtrim<vtmpl::string<VTMPL_SPLIT_##n(str, 0)>>::type
# define VTMPL_STRING(str) VTMPL_STRING_IMPL(str, 64)
# define VTMPL_STRING_256(str) VTMPL_STRING_IMPL(str, 256)
# define VTMPL_STRING_1024(str) VTMPL_STRING_IMPL(str, 1024)
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- #定义c-预处理器常量..我做错了什么
- 用C++中的一个变量定义一个常量
- 部分定义/别名模板模板参数
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- #为""定义宏;静态";针对不同的上下文
- 如何确保C++函数在定义之前声明(如override关键字)
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在命名空间中定义函数还是限定函数
- 此代码是否违反一个定义规则
- 编译C++时未定义的引用
- 不同翻译单元中不可重载的非内联函数定义
- 为什么在定义函数之前先声明它
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 使用boost :: mpl :: find_if带有自定义谓词
- 从 boost::mpl:vector 类型列表中定义成员变量
- 用户定义的MPL序列的文字:这合法吗
- 使用mpl::if_、boost::函数和void类型定义的问题
- 使用mpl::vector定义boost::变体类型