尝试调用hi
orstd::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
hi(int, char**)'`?