Question

Trying to invoke hi or std::move on it results in this:

/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;
}

Output:

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

It seems hi is almost the same as main except it doesn't give warnings like when using std::cout << hi. Is it possible to get this program to not output undefined reference tohi(int, char**)'`?

Was it helpful?

Solution

decltype(main) is an alias for the type int(int, char **). You have declared a function hi with this type but did not define it.

To get rid of the undefined reference error, just define the function:

int hi( int, char ** ) {
    return 0;
}

The messages that appear before true are not output from your program but from the compiler.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top