相关疑难解决方法(0)

constexpr是否支持lambda函数/表达式?

struct Test
{
  static const int value = []() -> int { return 0; } ();
};
Run Code Online (Sandbox Code Playgroud)

使用gcc-4.6我会得到类似的东西error: function needs to be constexpr.我已经尝试了多种组合放置constexpr在不同的地方,但没有运气.

constexpr支持lambda函数(不管return指定的类型与否)?什么是正确的语法?

任何可能的工作?

c++ lambda constexpr c++11

55
推荐指数
4
解决办法
2万
查看次数

lambda捕获的const int变量的值类别

我一直在试图理解何时何时不具有捕获默认odr的lambda-使用在其周围范围内定义的自动存储持续时间的变量(由此答案提示).在探索周围时,我偶然发现了一点点好奇心.GCC和Clang似乎不同意n以下代码中id-expression的值类别:

template <typename T> void assert_is_lvalue(const T&) {}
template <typename T> void assert_is_lvalue(const T&&) = delete;

int main() {
    const int n = 0;
    [=] { assert_is_lvalue(n); };
}
Run Code Online (Sandbox Code Playgroud)

Clang成功编译代码,而GCC不编译(error: use of deleted function).哪一个是正确的?或者这是未指定或实现定义的东西?

绑定对象的引用应该使用它,并且通过删除lambda的capture-default并观察两个编译器然后抱怨在n没有捕获默认值的情况下不能隐式捕获来确认.

将lambda标记为mutable编译器的输出没有明显的差别.

c++ lambda language-lawyer c++17 value-categories

8
推荐指数
1
解决办法
271
查看次数

与static_assert和boost :: hana相关的Clang编译错误

考虑以下在Clang 3.8上成功编译的问题-std=c++14.

#include <boost/hana.hpp>

namespace hana = boost::hana;

int main() {
    constexpr auto indices = hana::range<unsigned, 0, 3>();
    hana::for_each(indices, [&](auto i) {
        hana::for_each(indices, [&](auto j) {
            constexpr bool test = (i == (j == i ? j : i));
            static_assert(test, "error");
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

测试是非常不敏感的,但这不是重点.现在考虑一个替代版本,其中测试直接放在static_assert:

#include <boost/hana.hpp>

namespace hana = boost::hana;

int main() {
    constexpr auto indices = hana::range<unsigned, 0, 3>();
    hana::for_each(indices, [&](auto i) {
        hana::for_each(indices, [&](auto j) {
            static_assert((i == (j == i ? j …
Run Code Online (Sandbox Code Playgroud)

c++ static-assert clang++ boost-hana

5
推荐指数
1
解决办法
511
查看次数