C++11 Forward

 std::forword 用来转发函数调用的参数,按原样转发。

模板加std::forword为泛型编程提供了很大的方便,通过模板加std::forword实现一个通用的,可接收不同参数的统一方法调用。

#include <iostream>
#include <future>
#include <chrono>
#include <string>
#include<functional>

template<typename F, typename ... Types>
void test_forword(F f, Types ... args)
{
    auto func = std::bind(std::forward<F>(f), std::forward<Types>(args)...);
    func();
    
    /*using ret_type = typename std::result_of<F(Types...)>::type;
    std::packaged_task <ret_type()> task(func);

    std::future<ret_type> fret = task.get_future();
    std::thread t4(std::move(task));

    int ret = fret.get();
    std::cout << "forword bind task ret:" << ret << std::endl;

    t4.join();*/
}

int forword_task(int a, int b)
{
    std::cout << "forword_task a:" << a << std::endl;
    std::cout << "forword_task b:" << b << std::endl;
    return a + b;
}

class Forward
{
public:
    int forword_task(int a, int b)
    {
        std::cout << "forword_task a:" << a << std::endl;
        std::cout << "forword_task b:" << b << std::endl;
        return a + b;
    }
private:
    int c_ = 0;
};



int main()
{
    test_forword(forword_task, 10, 20);

    Forward fw;
    test_forword(&Forward::forword_task, &fw, 30, 40);
    return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Exploring C++ divides C++ up into bite-sized chunks that will help you learn the language one step at a time. Assuming no familiarity with C++, or any other C-based language, you’ll be taught everything you need to know in a logical progression of small lessons that you can work through as quickly or as slowly as you need. C++ can be a complicated language. Writing even the most straight-forward of programs requires you to understand many disparate aspects of the language and how they interact with one another. C++ doesn’t lend itself to neat compartmentalization the way other languages do. Rather than baffle you with complex chapters explaining functions, classes and statements in isolation we’ll focus on teaching you how to achieve results. By learning a little bit of this and a little of that you’ll soon have amassed enough knowledge to be writing non-trivial programs and will have built a solid foundation of experience that puts those previously baffling concepts into context. In this fully-revised second edition of Exploring C++, you’ll learn how to use the standard library early in the book. Next, you’ll learn to work with operators, objects and data-sources in increasingly realistic situations. Finally, you’ll start putting the pieces together to create sophisticated programs of your own design confident that you’ve built a firm base of experience from which to grow. What you’ll learn Learn how to use C++ from first principles in a practical hands-on way. Understand how to use Custom types, virtual functions and objects to structure your code Build your own function templates, namespaces and containers from the ground up. Put everything together to create sophisticated programs that work with pointers, dynamic memory and overloaded functions to achieve the results you want. Who this book is for Read this book if you want to learn C++ and have a basic understanding of how computer programs work. You don’t need to know a C-based language before you start, but a basic understanding of how programs are structured is helpful. Table of Contents Part 1: The Basics – Honing your tools Part 1: The Basics -Reading C++ Code Part 1: The Basics -Integer Expressions Part 1: The Basics -Strings Part 1: The Basics -Simple Input Part 1: The Basics -Error Messages Part 1: The Basics -For Loops Part 1: The Basics -Formatted Output Part 1: The Basics -Arrays and Vectors Part 1: The Basics -Incrementand Decrement Part 1: The Basics -Conditions and Logic Part 1: The Basics -Compound Statements Part 1: The Basics -Introduction to File I/O Part 1: The Basics -The Map Data Structure Part 1: The Basics -Type Synonyms Part 1: The Basics -Characters Part 1: The Basics -Character Categories Part 1: The Basics -Case-Folding Part 1: The Basics -Writing Functions Part 1: The Basics -Function Arguments Part 1: The Basics -Using Algorithms Part 1: The Basics -Overloading Function Names Part 1: The Basics -Big and Little Numbers Part 1: The Basics -Very Big and Very Little Numbers Part 1: The Basics -Documentation Part 1: The Basics -Project1: Body-Mass Index Part 2: Custom Types – Custom Types Part 2: Custom Types – Overloading Operators Part 2: Custom Types – Custom I/O Operators Part 2: Custom Types – Assignment and Initialization Part 2: Custom Types – Writing Classes Part 2: Custom Types – More About Member Functions Part 2: Custom Types – Access Levels Part 2: Custom Types – Introduction to Object-Oriented Programming Part 2: Custom Types – Inheritance Part 2: Custom Types – Virtual Functions Part 2: Custom Types – Classes and Types Part 2: Custom Types – Declarations and Definitions Part 2: Custom Types – Using Multiple Source Files Part 2: Custom Types – Function Objects Part 2: Custom Types – Useful Algorithms Part 2: Custom Types – Iterators Part 2: Custom Types – Exceptions Part 2: Custom Types – More Operators Part 2: Custom Types – Project2: Fixed-point Numbers Part 3: Generic Programming – Function Templates Part 3: Generic Programming – Class Templates Part 3: Generic Programming – Template Specialization Part 3: Generic Programming – Partial Specialization Part 3: Generic Programming – Names and Namespaces Part 3: Generic Programming – Containers Part 3: Generic Programming – International Characters Part 3: Generic Programming – Locales and Facets Part 3: Generic Programming – TextI/O Part 3: Generic Programming – Project3: Currency Type Part 4: Real Programming – Pointers Part 4: Real Programming – Dynamic Memory Part 4: Real Programming – Exception-Safety Part 4: Real Programming – Old-Fashioned Arrays Part 4: Real Programming – SmartPointers Part 4: Real Programming – Working with Bits Part 4: Real Programming – Enumerations Part 4: Real Programming – Multiple Inheritance Part 4: Real Programming – Traits and Policies Part 4: Real Programming – Names and Templates Part 4: Real Programming – Overloaded Functions Part 4: Real Programming – Metaprogramming Part 4: Real Programming – Project4: Calculator Book Details Paperback: 656 pages Publisher: Apress; 2 edition (December 19, 2013) Language: English ISBN-10: 1430261935 ISBN-13: 978-1430261933

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tangcpp

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值