decltype(main)
是类型的别名 int(int, char **)
. 。您已经声明了功能 hi
使用这种类型,但没有定义它。
摆脱 undefined reference
错误,只需定义函数:
int hi( int, char ** ) {
return 0;
}
出现之前出现的消息 true
不是从程序中输出,而是从编译器中输出。
题
试图调用 hi
或者 std::move
在此导致这样做:
/tmp/cch3DRvH.o: In function `main':
main.cpp:(.text.startup+0x5): undefined reference to `hi(int, char**)'
collect2: error: ld returned 1 exit status
#include <type_traits>
#include <utility>
#include <iostream>
#include <typeinfo>
#include <functional>
int main(int argc, char* argv[])
{
decltype(main) hi;
decltype(main) hi2;
// std::function<int(int, char**)> hi2 = std::move(hi);
// hi(argc, argv);
std::cout << std::boolalpha;
std::cout << std::is_same<decltype(main), decltype(hi)>::value << std::endl;
std::cout << std::is_function<decltype(main)>::value << std::endl;
std::cout << std::is_function<decltype(hi)>::value << std::endl;
std::cout << std::is_same<decltype(std::move(main)), decltype(std::move(hi))>::value << std::endl;
std::cout << std::is_same<decltype(hi2), decltype(hi)>::value << std::endl;
return 0;
}
输出:
main.cpp: In function ‘int main(int, char**)’:
main.cpp:16:54: warning: ISO C++ forbids taking address of function ‘::main’ [-Wpedantic]
std::cout << std::is_same<decltype(std::move(main)), decltype(std::move(hi))>::value << std::endl;
main.cpp:16:54: warning: ISO C++ forbids taking address of function ‘::main’ [-Wpedantic]
true
true
true
true
true
它似乎 hi
几乎与 main
除了使用时没有发出警告 std::cout << hi
. 。是否有可能使该程序不输出 undefined reference to
嗨(int,char **)'?
解决方案
decltype(main)
是类型的别名 int(int, char **)
. 。您已经声明了功能 hi
使用这种类型,但没有定义它。
摆脱 undefined reference
错误,只需定义函数:
int hi( int, char ** ) {
return 0;
}
出现之前出现的消息 true
不是从程序中输出,而是从编译器中输出。