fmtlib / fmt

A modern formatting library
https://fmt.dev
Other
19.84k stars 2.42k forks source link

Update checks for dynamic_cast usage when compiled with no rtti #3963

Closed edo9300 closed 1 month ago

edo9300 commented 1 month ago

The FMT_USE_TYPEID macro was renamed to FMT_HAS_RTTI and moved to fmt/base.h so that it can be used in other parts of the library. That is now used to check wheter or not dynamic_cast should be used in the codebase, currently the only instances were in write_ostream_unicode. This allows the library to be compiled properly with rtti disabled.

vitaut commented 1 month ago

Could you provide a godbolt repro for the ostream issue?

edo9300 commented 1 month ago

Could you provide a godbolt repro for the ostream issue?

It was more from a warning raised by visual studio when building my project with the latest fmt C:\vcpkg2\installed\x86-windows-static\include\fmt\ostream.h(77,19): warning C4541: 'dynamic_cast' used on polymorphic type 'std::basic_streambuf<char,std::char_traits<char>>' with /GR-; unpredictable behavior may result, anyways I managed to generate a test case where this scenario actually occurs, since the code with dynamic cast is windows only, i'll just put the sample code here

#define FMT_HEADER_ONLY
#include <fmt/ostream.h>
#include <iostream>

struct custom_ostream :  private std::streambuf , public std::ostream
{
    custom_ostream() : std::ostream(this) {}
private:
    int overflow(int c) override
    {
        std::cout.put(c);
        return 0;
    }
};

int main()
{
    custom_ostream b;
    fmt::print(b, "working\n");
    return 0;
}

Building it as cl test.cpp /GR- /utf-8 /EHsc /I "fmt\include\path" will produce that warning, and when executing nothing will be printed (at least on my machine), building it with rtti on cl test.cpp /utf-8 /EHsc /I "fmt\include\path" will instead properly print the output to the console. This sample has been built with visual studio 2019.

vitaut commented 1 month ago

Merged, thanks.