相关疑难解决方法(0)

对于const的引用,std :: is_const的等价物是什么?

考虑一下代码:

int const  x = 50;
int const& y = x;
cout << std::is_const<decltype(x)>::value << endl; // 1
cout << std::is_const<decltype(y)>::value << endl; // 0
Run Code Online (Sandbox Code Playgroud)

这是有道理的,因为y不是const参考,它是对a的引用const.

有没有foo这样std::foo<decltype(y)>::value的1?如果没有,那么定义我自己的是什么样的呢?

c++ types const

21
推荐指数
1
解决办法
1244
查看次数

GCC无法使用init-capture捕获模板化类型的"this"指针

模板化的类可以this在lambda中捕获自己的指针:

template <typename T>
class Foo {
  public:
    void foo(void) {}
    auto getCallableFoo(void) {
      return [this]() { this->foo(); };
    }
};
Run Code Online (Sandbox Code Playgroud)

Foo可以使用以下代码测试此示例和所有其他示例:

int main()
{
  Foo<int> f;
  auto callable = f.getCallableFoo();
  callable();
}
Run Code Online (Sandbox Code Playgroud)

但是,如果使用init-capture,则不再适用于GCC:

    auto getCallableFoo(void) {
      return [ptr = this]() { ptr->foo(); };
    }
Run Code Online (Sandbox Code Playgroud)

错误消息(来自GCC 5.1):

error: ‘Foo<T>::getCallableFoo()::<lambda()>::__ptr’ has incomplete type
Run Code Online (Sandbox Code Playgroud)

Clang 3.7似乎编译并运行此代码而没有错误.(我实际上是在使用3.7之前从源代码编译的版本,但我不认为从那时起它已经破坏了.)

初始捕获应该像赋值一样auto,但是下面的代码似乎在GCC中没有错误地工作:

// New method in Foo:
auto getPtr(void) {
  return this;
}

// Usage:
auto ptr = f.getPtr();
ptr->foo();
Run Code Online (Sandbox Code Playgroud)

那么为什么ptr …

c++ lambda inheritance templates c++14

12
推荐指数
1
解决办法
263
查看次数

标签 统计

c++ ×2

c++14 ×1

const ×1

inheritance ×1

lambda ×1

templates ×1

types ×1