小编and*_*gor的帖子

按类型实例化C++ lambda

我想要一种从函数中制作仿函数的方法.现在我尝试通过lambda函数包装函数调用并稍后实例化它.但编译器说比删除lambda构造函数.那么有什么方法可以编译这段代码吗?或者也许是另一种方式?

#include <iostream>  

void func()
{
    std::cout << "Hello";
}

auto t = []{ func(); };
typedef decltype(t) functor_type;

template <class F>
void functor_caller()
{
    F f;
    f();
}

int main()
{
    functor_caller<functor_type>();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在我得到这样的编译器错误:

error: use of deleted function  '<lambda()>::<lambda>()'

error: a lambda closure type has a deleted default constructor
Run Code Online (Sandbox Code Playgroud)

在我看来,唯一的方法是使用宏:

#define WRAP_FUNC(f) \
struct f##_functor       \
{                       \
    template <class... Args >                             \
    auto operator()(Args ... args) ->decltype(f(args...)) \
    {                                                     \
        return f(args...);                                \
    }                                                     \ …
Run Code Online (Sandbox Code Playgroud)

c++ lambda instantiation c++11

21
推荐指数
4
解决办法
3142
查看次数

在GCC复合语句表达式中调用return

我可以安全地在GCC复合语句表达式中使用return吗?

例如,我定义宏

#define CHECK_FUNC_RESULT(func)    \
({                                 \
   int result = func();            \
   if (!result) return;            \
   result;                         \
})
Run Code Online (Sandbox Code Playgroud)

并以这种方式在代码中的某处使用它:

int function1() 
{
   if (some_condition) 
     return 0;
   ...
   return 1; 
}

void function2()
{
   if(CHECK_FUNC_RESULT(function1)) {
      ... to do something
   }
}
Run Code Online (Sandbox Code Playgroud)

我可以期待从function2(在some_condition == true)返回而没有任何未定义的行为吗?

gcc expression

7
推荐指数
1
解决办法
417
查看次数

自由函数中静态变量与静态成员函数的区别

我有这样的代码:

struct Storage
{
    static int GetData()
    {
            static int global_value;
            return global_value++;
    }
};
int free_func()
{
    static int local_value;
    return local_value++;
}

int storage_test_func()
{
    return Storage::GetData();
}
Run Code Online (Sandbox Code Playgroud)

在OSX上编译它:

$ clang++ 1.cpp -shared
Run Code Online (Sandbox Code Playgroud)

并运行nm:

$ nm | c++filt
Run Code Online (Sandbox Code Playgroud)

我得到了奇怪的结果:

0000000000000f50 T storage_test_func()
0000000000000f30 T free_func()
0000000000000f60 unsigned short Storage::GetData()
0000000000001024 bool free_func()::local_value
0000000000001020 D Storage::GetData()::global_value
                 U dyld_stub_binder
Run Code Online (Sandbox Code Playgroud)

两个符号(local_valueglobal_value)有不同的联系!一个明显的区别是global_value静态成员函数local_value中定义的并且在自由函数中定义.

有人能解释为什么会这样吗?

UPD:

评论阅读之后看起来我应该澄清一些事情.也许使用c++filt是一个坏主意.没有它,它显示:

$ nm

0000000000000f50 T __Z17storage_test_funcv
0000000000000f30 T …
Run Code Online (Sandbox Code Playgroud)

c++ static-methods linkage

7
推荐指数
2
解决办法
512
查看次数

如何为qmake中的不同目标指定单独的编译选项?

有没有办法为qmake中的不同目标指定单独的编译选项?

例如:

QMAKE_CXXFLAGS += -O 
SOURCES += file1.cpp    

QMAKE_CXXFLAGS += -std=gnu++0x -O 
SOURCES += file2.cpp
Run Code Online (Sandbox Code Playgroud)

因此file1.cpp将仅使用-O选项编译,文件file2.cpp使用-std = gnu ++ 0x -O选项编译.

qmake

6
推荐指数
1
解决办法
1395
查看次数

从另一个线程原子取消 asio 异步计时器

我有一个定期运行的 boost period_timer (例如http://www.boost.org/doc/libs/1_35_0/doc/html/boost_asio/tutorial/tuttimer3/src.html):

#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

void print(const boost::system::error_code& /*e*/,
    boost::asio::deadline_timer* t)
{
    t->expires_at(t->expires_at() + boost::posix_time::seconds(1));
    t->async_wait(boost::bind(print,
          boost::asio::placeholders::error, t, count));
}

int main()
{
  boost::asio::io_service io;

  boost::asio::deadline_timer t(io, boost::posix_time::seconds(1));
  t.async_wait(boost::bind(print,
        boost::asio::placeholders::error, &t));

  io.run();

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在我需要从另一个线程取消它。但是,如果 cancel 的调用恰好出现在 print 函数执行期间但在 expires_at 调用之前呢?然后定时器将继续运行。

处理它的一种方法是运行类似的东西

while (timer.cancel() == 0) {
}
Run Code Online (Sandbox Code Playgroud)

在那个单独的线程函数中。

但也许有人知道解决这个问题的更优雅的方式?

multithreading boost timer cancellation

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