0

我希望我的函数通过将另一个函数作为参数get_source来返回一个类型。std::source_location

例如,我有一个名为helloMessageand的函数,sumOf它具有以下参数类型和返回类型:

void helloMessage(const char* message);
int sumOf(int num1, int num2); 

假设我有一个函数,它将从函数对象获取源位置,然后返回其值:

template <typename function_t>
std::source_location get_source(function_t func);

输入:

auto info_1 = get_source(helloMessage);
auto info_2 = get_source(sumOf); 

std::cout 
<< "info_1: " << info_1.function_name() << '\n'
<< "info_2: " << info_2.function_name() << '\n';

这是我的预期输出:

void helloMessage(const char*)
int sumOf(int, int)

我怎样才能做到这一点?

4

1 回答 1

1

source_location::current永远是任何source_location对象的最终来源。认真对待创建该对象的代码源中的位置source_location

因此,无法将函数转换为它来自的源中的位置。除非该函数对象已将该函数的源位置存储在某处,否则这将非常困难。

于 2021-05-02T13:31:57.723 回答