我正在尝试使用 C++17 的 constexpr lambda 来获取编译时字符串:
#include <utility>
template <char...>
struct str
{
constexpr auto operator==(const str&) const { return true; }
void foo() const;
};
template <typename S, std::size_t... Ns>
constexpr auto make_str(S s, std::index_sequence<Ns...>)
{
return str<s()[Ns]...>{};
}
#define LIT(s) \
make_str([]() { return s; }, std::make_index_sequence<sizeof(s) - 1>{})
constexpr auto x = LIT("hansi");
constexpr auto y = x;
static_assert(x == y);
到目前为止看起来不错。但后来我尝试调用成员函数:
x.foo();
使用主干中的当前 gcc (g++ (GCC) 7.0.0 20161102),我收到以下错误消息:
c.cpp:19:1: error: ‘x’ does not name a type; did you mean ‘x’?
x.foo();
See https://godbolt.org/g/uN25e1 https://godbolt.org/g/uN25e1进行演示
因为我什至没有尝试使用x
作为一种类型,这让我觉得很奇怪。
这是编译器错误吗?或者是x
真的很奇怪吗?
正如其他人的评论所指出的,这只是调用函数名称空间范围的结果。
恕我直言,尽管如此,错误消息相当模糊。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)