Skip to content

Cannot print function pointer #68

Open
@evandrocoan

Description

@evandrocoan
#include "tinyformat.h"

int main(int argc, char const *argv[])
{
    // typedef int (*Function)( int argc, char const *argv[] );
    std::cerr << tfm::format( "var2 %s", &main ) << std::endl;
    return 0;
}

Results in:

# g++ -o main -g -ggdb test_debugger.cpp --std=c++98 && ./main
var2 true

If I remove the &main and use only main, it does not compile:

# g++ -o main -g -ggdb test_debugger.cpp ---std=c++98 && ./main
In file included from test_debugger.cpp:25:0:
tinyformat.h: In instantiation of ‘static void tinyformat::detail::FormatArg::formatImpl(std::ostream&, const char*, const char*, int, const void*) [with T = int(int, const char**); std::ostream = std::basic_ostream<char>]’:
tinyformat.h:499:38:   required from ‘tinyformat::detail::FormatArg::FormatArg(const T&) [with T = int(int, const char**)]’
tinyformat.h:976:9:   required from ‘void tinyformat::detail::FormatListN<N>::init(int, const T0&) [with T0 = int(int, const char**); int N = 1]’
tinyformat.h:976:9:   required from ‘tinyformat::detail::FormatListN<N>::FormatListN(const T0&) [with T0 = int(int, const char**); int N = 1]’
tinyformat.h:1025:1:   required from ‘tinyformat::detail::FormatListN<1> tinyformat::makeFormatList(const T1&) [with T1 = int(int, const char**)]’
tinyformat.h:1108:1:   required from ‘void tinyformat::format(std::ostream&, const char*, const T1&) [with T1 = int(int, const char**); std::ostream = std::basic_ostream<char>]’
tinyformat.h:1108:1:   required from ‘std::__cxx11::string tinyformat::format(const char*, const T1&) [with T1 = int(int, const char**); std::__cxx11::string = std::__cxx11::basic_string<char>]’
test_debugger.cpp:30:47:   required from here
tinyformat.h:522:57: error: invalid static_cast from type ‘const void*’ to type ‘int (*)(int, const char**)’
             formatValue(out, fmtBegin, fmtEnd, ntrunc, *static_cast<const T*>(value));
                                                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
tinyformat.h: In instantiation of ‘static int tinyformat::detail::FormatArg::toIntImpl(const void*) [with T = int(int, const char**)]’:
tinyformat.h:499:38:   required from ‘tinyformat::detail::FormatArg::FormatArg(const T&) [with T = int(int, const char**)]’
tinyformat.h:976:9:   required from ‘void tinyformat::detail::FormatListN<N>::init(int, const T0&) [with T0 = int(int, const char**); int N = 1]’
tinyformat.h:976:9:   required from ‘tinyformat::detail::FormatListN<N>::FormatListN(const T0&) [with T0 = int(int, const char**); int N = 1]’
tinyformat.h:1025:1:   required from ‘tinyformat::detail::FormatListN<1> tinyformat::makeFormatList(const T1&) [with T1 = int(int, const char**)]’
tinyformat.h:1108:1:   required from ‘void tinyformat::format(std::ostream&, const char*, const T1&) [with T1 = int(int, const char**); std::ostream = std::basic_ostream<char>]’
tinyformat.h:1108:1:   required from ‘std::__cxx11::string tinyformat::format(const char*, const T1&) [with T1 = int(int, const char**); std::__cxx11::string = std::__cxx11::basic_string<char>]’
test_debugger.cpp:30:47:   required from here
tinyformat.h:528:45: error: invalid static_cast from type ‘const void*’ to type ‘int (*)(int, const char**)’
             return convertToInt<T>::invoke(*static_cast<const T*>(value));
                                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

Example using a volatile variable:

#include "tinyformat.h"

int main(int argc, char const *argv[])
{
    typedef int (*Function)( int argc, char const *argv[] );
    volatile Function var1 = reinterpret_cast< volatile Function >( main );

    std::cerr << tfm::format( "volatile var1 %p", var1 ) << std::endl;
    std::cerr << tfm::format( "volatile var1 %s", var1 ) << std::endl;
    return 0;
}

Results in the same

# g++ -o main -g -ggdb test_debugger.cpp --std=c++98 && ./main
volatile var1 1
volatile var1 true

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions