小编eg0*_*x20的帖子

为什么lambda不会从达到范围捕获类型const double,但const int是?

我似乎无法理解为什么下面的类型为const int的代码编译:

int main()
{
  using T = int;
  const T x = 1;
  auto lam = [] (T p) { return x+p; };
}
$ clang++ -c lambda1.cpp  -std=c++11
$
Run Code Online (Sandbox Code Playgroud)

而这个类型为const double的那个不是:

int main()
{
  using T = double;
  const T x = 1.0;
  auto lam = [] (T p) { return x+p; };
}
$ clang++ -c lambda2.cpp  -std=c++11
lambda1.cpp:5:32: error: variable 'x' cannot be implicitly captured in a lambda with no capture-default specified
  auto lam = [] …
Run Code Online (Sandbox Code Playgroud)

c++ lambda constexpr c++11

12
推荐指数
2
解决办法
832
查看次数

std :: future和clang with -stdlib = libstdc ++

以下程序无法与clang和-stdlib = libstdc ++链接:

$ cat future.cpp
#include <iostream>
#include <future>

int main()
{
  std::future<int> f1 = std::async([](){ return 42; });
  f1.wait();
  std::cout << "Magic number is: " << f1.get() << std::endl;
}
$ g++-mp-5 future.cpp -std=c++11 && ./a.out
Magic number is: 42
$ clang++-mp=3.5 future.cpp -std=c++11 && ./a.out
Magic number is: 42
Run Code Online (Sandbox Code Playgroud)

使用clang和-stdlib = libstdc ++构建时,会发生以下链接错误:

$ clang++-mp-3.5  future.cpp -std=c++11 -stdlib=libstdc++ -I/opt/local/include/gcc5/c++ -I/opt/local/include/gcc5/c++/x86_64-apple-darwin14 -L/opt/local/lib/gcc5 -lstdc++ && ./a.out 
Undefined symbols for architecture x86_64:
  "std::__once_call", referenced from:
      void std::call_once<void (std::__future_base::_State_baseV2::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> …
Run Code Online (Sandbox Code Playgroud)

future clang libstdc++ c++11

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

标签 统计

c++11 ×2

c++ ×1

clang ×1

constexpr ×1

future ×1

lambda ×1

libstdc++ ×1