Domanda

Cercando di invocare hi o std::move su di esso si traduce in questo:

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

Produzione:

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

Sembra hi è quasi lo stesso di main Tranne che non dà avvertimenti come quando si usa std::cout << hi. È possibile ottenere questo programma non output undefined reference tociao (int, char **) '`?

È stato utile?

Soluzione

decltype(main) è un alias per il tipo int(int, char **). Hai dichiarato una funzione hi Con questo tipo ma non lo ha definito.

Per sbarazzarsi del undefined reference Errore, basta definire la funzione:

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

I messaggi che appaiono prima true non sono output dal programma ma dal compilatore.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top