4

C++:std::function与模板的结合

 3 years ago
source link: https://www.lpime.cn/article/112
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
C++:std::function与模板的结合 - Ryan_naiquan

function模板C++

帮助博客

#include <iostream>
#include <functional>

void sayHello()
{
	std::cout << "aaaa" << std::endl;
}

int sayHi()
{
	std::cout << "bbbb" << std::endl;
	return 11;
}

template<typename R = void, typename... Args>
class Fn {
public:
	Fn(std::function<R(Args...)> fun) : _fun(fun) 
	{
	}

	R operator()(Args... args) 
	{
		return _fun(args...);
	}
private:
	std::function<R(Args...) > _fun;
};

template <typename T>
class Func {
public:

	Func(T fun) {
		if (!fun) {
			throw "fun nullptr";
		}
		_fun = fun;
	}

	template<typename R, typename A1, typename A2, typename A3, typename A4, typename A5>
	R Call(A1 a1, A2 a2, A3 a3, A4 a4, A5 a5) {
		return _fun(a1, a2, a3, a4, a5);
	}

	template<typename R, typename A1, typename A2, typename A3, typename A4>
	R Call(A1 a1, A2 a2, A3 a3, A4 a4) {
		return _fun(a1, a2, a3, a4);
	}

	template<typename R, typename A1, typename A2, typename A3>
	R Call(A1 a1, A2 a2, A3 a3) {
		return _fun(a1, a2, a3);
	}

	template<typename R, typename A1, typename A2>
	R Call(A1 a1, A2 a2) {
		return _fun(a1, a2);
	}

	template<typename R, typename A1>
	R Call(A1 a1) {
		return _fun(a1);
	}

	template<typename R>
	R Call() {
		return _fun();
	}

	void Call() {
		_fun();
	}

private:
	T _fun;
};

int sum(int i, int j, int k) {
	return i + j + k;
}

int main()
{
	Func<int (*)(int, int, int) > sumFunc(sum);
	std::cout << "sumFunc.Call<int>(1, 2, 3) : " << sumFunc.Call<int>(1, 2, 3) << std::endl;

	Fn<> sayHelloFn(sayHi);
	sayHelloFn();
	return 0;
}
 Func对象例用call的重载实现,Fn使用变参和仿函数实现。

本文由 Ryan 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: 2021/05/20 14:42


Recommend

  • 34
    • www.tuicool.com 6 years ago
    • Cache

    Functions in std

    Names of functions from the Standard Library used as pointers/references to functions can cause breakage in your code if you are upgrading to the newer version of C++ or compiling using a different implementation of the S...

  • 43
    • www.tuicool.com 6 years ago
    • Cache

    Using C++17 std::optional

    Let's take a pair of two types <YourType, bool> — what can you do with such composition? In this article, I'll describe std:optional — a new helper type added in C++17. I...

  • 43
    • www.tuicool.com 6 years ago
    • Cache

    std::optional: How, when, and why

    This post is part of a regular series of posts where the C++ product team here at Microsoft and other guests answer questions we have received from customers. The questions can be about anything C++ related: MSVC toolset,...

  • 24
    • www.tuicool.com 5 years ago
    • Cache

    std::any: How, when, and why

    This post is part of a  regular series of posts ...

  • 38

    Sometimes we need unformatted data, simple byte sequences. At first glance, std::string might be a fitting data structure for that, but it is not. Think about data we get from networks, a CAN...

  • 33
    • www.tuicool.com 5 years ago
    • Cache

    (Not) using namespace std;

    using namespace std; This is called a using directive . Using it in code that is supposed to work for years is not a good idea. Sometimes programmers are given advice to just type...

  • 43
    • www.tuicool.com 5 years ago
    • Cache

    SSO of std::string

    No, not single sign-on :stuck_out_tongue: Short string optimization. But before I get into that, little rant… F**K Windows :rage: ! I’ve tried, I really tried to build all my codi...

  • 29
    • www.tuicool.com 5 years ago
    • Cache

    std::format in C++20

    I’m happy to announce that the Text Formatting proposal ( std::format...

  • 37

    Posted by Florian Gilcher on November 07, 2019 under release

  • 2
    • forrestsu.github.io 2 years ago
    • Cache

    C++11 std::function 和 std::bind

    C++11 std::function 和 std::bind 2017年6月9日 | 字数 871 |

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK