小编Dav*_*ian的帖子

如果(条件)在C++中尝试{...}合法吗?

例如:

if (true) try
{
    // works as expected with both true and false, but is it legal?
}
catch (...)
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)

换句话说,将if -block放在if条件之后是否合法?

c++ exception try-catch language-lawyer

44
推荐指数
4
解决办法
5997
查看次数

具有自动功能的C++ 17模板参数是否允许受约束的std :: function对象?

随着即将推出的带有auto非类型模板参数的 C++ 17特性,是否可以以std::function能够放置例如以下函数的方式实现:

    bool f(int n,  double d) {}    
    bool g(bool b, char c)   {}
    bool h(bool b)           {}
Run Code Online (Sandbox Code Playgroud)

进入自动模板化的std::function对象:

   std::function<bool(auto,   auto)> faa = f; // ok
   std::function<bool(int,    auto)> fia = f; // ok
   std::function<bool(double, auto)> fda = f; // error: function type mismatch
   std::function<bool(auto,   auto)> gaa = g; // ok
   std::function<bool(auto,   auto)> haa = h; // error: function type mismatch
   std::function<bool(auto)>         ha  = h; // ok
Run Code Online (Sandbox Code Playgroud)

等等.

换句话说,让std::function对象受限于他们接受的函数类型? …

c++ templates auto std-function c++17

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

究竟何时初始化在全局范围内声明的thread_local变量?

例如:

#include <thread>

thread_local int n = 1;

void f()
{
    ++n; // is n initialized here for each thread or prior to entering f()?
}

int main()
{
    std::thread ta(f);
    std::thread tb(f);

    ta.join();
    tb.join();
}
Run Code Online (Sandbox Code Playgroud)

它仍然是不完全清楚,从这里为n初始化时。

c++ storage multithreading initialization

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