小编use*_*543的帖子

嵌套绑定表达式

这是我上一个问题的后续问题.

#include <functional>

int foo(void) {return 2;}

class bar {
public:
    int operator() (void) {return 3;};
    int something(int a) {return a;};
};

template <class C> auto func(C&& c) -> decltype(c()) { return c(); }

template <class C> int doit(C&& c) { return c();}

template <class C> void func_wrapper(C&& c) { func( std::bind(doit<C>, std::forward<C>(c)) ); }

int main(int argc, char* argv[])
{
    // call with a function pointer
    func(foo);
    func_wrapper(foo);  // error

    // call with a member …
Run Code Online (Sandbox Code Playgroud)

c++ bind c++11

13
推荐指数
1
解决办法
2573
查看次数

模板,函数指针和C++ 0x

我个人实验之一,了解一些C++ 0x特性:我正在尝试将函数指针传递给模板函数来执行.最终执行应该发生在不同的线程中.但是对于所有不同类型的函数,我无法使模板工作.

#include <functional>

int foo(void) {return 2;}

class bar {
public:
    int operator() (void) {return 4;};
    int something(int a) {return a;};
};

template <class C>
int func(C&& c)
{
    //typedef typename std::result_of< C() >::type result_type;
    typedef typename std::conditional< 
        std::is_pointer< C >::value,
        std::result_of< C() >::type,
        std::conditional<
            std::is_object< C >::value,
            std::result_of< typename C::operator() >::type,
            void>
        >::type result_type;
    result_type result = c();
    return result;
}

int main(int argc, char* argv[])
{
    // call with a function pointer
    func(foo);

    // call with a …
Run Code Online (Sandbox Code Playgroud)

c++ templates function-pointers c++11

9
推荐指数
1
解决办法
1444
查看次数

标签 统计

c++ ×2

c++11 ×2

bind ×1

function-pointers ×1

templates ×1