问题描述
#include< utility>< p>
模板< char ...>
struct str
{
constexpr auto operator ==(const str& amp; amp; const)const {return true; }
void foo()const;
};
模板< 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();
使用来自trunk(g ++(GCC)7.0.0 20161102)的当前gcc,我得到以下错误消息:
c.cpp:19:1:错误:'x'不会命名一个类型;你的意思是'x'吗?
x.foo();
请参阅进行演示
由于我甚至不想使用 x
作为一个类型,这让我感到奇怪。
这是一个编译器错误吗?或者是 x
真的很奇怪吗?
其他人的评论,这只是调用一个函数命名空间范围的结果。
恕我直言,错误信息是相当模糊的,虽然。
I am experimenting with C++17's constexpr lambdas to get compile time strings:
#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);
Looks good so far. But then I tried calling a member function:
x.foo();
Using the current gcc from trunk (g++ (GCC) 7.0.0 20161102), I get the following error message:
c.cpp:19:1: error: ‘x’ does not name a type; did you mean ‘x’?
x.foo();
See https://godbolt.org/g/uN25e1 for a demo
Since I am not even trying to use x
as a type, this strikes me as weird.
Is that a compiler bug? Or is x
something really strange?
As pointed out in the comments by others, this is just a result of calling a function namespace scope.
IMHO, the error message is quite obscure, though.
这篇关于constexpr lambda /'x'不会命名一个类型;你的意思是'x'吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!