c42f / tinyformat

Minimal, type safe printf replacement library for C++
531 stars 75 forks source link

Allow to print any free or member class pointer #73

Open evandrocoan opened 4 years ago

evandrocoan commented 4 years ago

fix #68 - Cannot print function pointer

#include<iostream>
#include "tinyformat.h"

struct test_debugger { void var() {} };
void fun_void_void(){};
void fun_void_double(double d){};
double fun_double_double(double d){return d;}

int main(void) {
    int* var;

    std::cout << std::boolalpha;
    std::cout << tfm::format( "0. %s", &var ) << std::endl;
    std::cout << tfm::format( "1. %s", &fun_void_void ) << std::endl;
    std::cout << tfm::format( "2. %s", &fun_void_double ) << std::endl;
    std::cout << tfm::format( "3. %s", &fun_double_double ) << std::endl;
    std::cout << tfm::format( "4. %s", &test_debugger::var ) << std::endl;
    return 0;
}

Prints:

$ g++ -o main.exe -g -ggdb test.cpp --std=c++98 && ./main.exe
0. 0xffffcbb0
1. 0x100401100
2. 0x100401107
3. 0x100401113
4. 0x100405f10

$ g++ -o main.exe -g -ggdb test.cpp --std=c++11 && ./main.exe
0. 0xffffcbb0
1. 0x100401100
2. 0x100401107
3. 0x100401113
4. 0x100405b70
evandrocoan commented 4 years ago

Passing all tests now.