问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/40374191/constexpr-lambda-x-does-not-name-a-type-did-you-mean-x