constexpr lambda / ‘x’ does not name a type; did you mean ‘x’?

三世轮回 提交于 2020-01-03 17:10:48

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!