Last active
August 12, 2021 07:45
-
-
Save talybin/70eb30cfe59d92bc33d07c036157d50a to your computer and use it in GitHub Desktop.
Function taking any argument
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <functional> | |
#include <any> | |
struct any_type : std::any | |
{ | |
using std::any::any; | |
template <class T> operator T() const { | |
return std::any_cast<T>(*this); | |
} | |
}; | |
using function_one = | |
std::function<void(any_type)>; | |
int main() | |
{ | |
function_one fn; | |
fn = [](double x) { | |
std::cout << x << '\n'; | |
}; | |
fn(3.14); | |
fn = [](const char* str) { | |
std::cout << str << '\n'; | |
}; | |
fn("test"); | |
} |
With return type
using function_one = std::function<any_type(any_type)>;
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Actually this is an expanded version of
std::function<void(std::any)>
wherestd::any
does not have a cast operator. The only acceptable argument isauto
orstd::any
.