加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – constexpr lambda /’x’没有命名类型;你的意思是’x’?

发布时间:2020-12-16 06:52:47 所属栏目:百科 来源:网络整理
导读:我正在尝试使用C 17的constexpr lambdas来获取编译时字符串: #include utilitytemplate char...struct str{ constexpr auto operator==(const str) const { return true; } void foo() const;};template typename S,std::size_t... Nsconstexpr auto make_s
我正在尝试使用C 17的constexpr lambdas来获取编译时字符串:

#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();

使用来自trunk(g(GCC)7.0.0 20161102)的当前gcc,我收到以下错误消息:

c.cpp:19:1: error: ‘x’ does not name a type; did you mean ‘x’?
 x.foo();

有关演示,请参见https://godbolt.org/g/uN25e1

由于我甚至没有尝试使用x作为一种类型,这让我觉得奇怪.

这是编译器错误吗?还是x真的很奇怪?

解决方法

正如其他人的评论所指出的,这只是调用函数命名空间范围的结果.

恕我直言,错误信息是相当模糊的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读