tfc / pprintpp

Typesafe Python Style Printf Formatting for C++
MIT License
233 stars 12 forks source link

C++11 Lambda expression instead of local class/struct in AUTOFORMAT #13

Closed HpLightcorner closed 4 years ago

HpLightcorner commented 4 years ago

Hey, Will trying to use pprintpp with MSVC, CLANG and Compilers for Microcontrollers (Aurix TriCore) I had some issues with the AUTOFORMAT Macro - not every Compiler seems to support the "local class" scheme used with the ({struct....}) statement.

Hence, I modified the AUTOFORMAT Macro to use a lambda expression which gets called - I am not aware if this will cause performance-issues, so maybe you can give me additional advice?

This approach is compatible with at least all the compilers I have tried (especially MSVC (Visual Studio Build Tools 2019) caused problems).

Here my modification:

#define AUTOFORMAT(s, ...) \
        []() {                                                                              \
            struct strprov                                                                  \
            {                                                                               \
                static constexpr const char *str() { return static_cast<const char *>(s); } \
            };                                                                              \
            using paramtypes = decltype(pprintpp::tie_types(__VA_ARGS__));                  \
            using af = pprintpp::autoformat_t<strprov, paramtypes>;                         \
            return af::str();                                                               \
        }()

#define pprintf(s, ...) printf(AUTOFORMAT(s, ## __VA_ARGS__), ## __VA_ARGS__);

}

If you are interested, I could do a fork and make a pull request.

Kind regards

HpLightcorner commented 4 years ago

Ah, I checked the Windows branch right now - it is already using a lambda - sorry for not checking that before...! At least I can Inform you that the Compilers for Aurix will also work when using the Lambda - I will check if it still works with the Windows-Branch implementation.

I will close the issue as my findings are already implemented in the open pull-request #12 Thanks for your work!!

DBJDBJ commented 4 years ago

Many thanks @HpLightcorner , that is indeed in. I also plan to add wchar_t support so that you can do unicode the Windows way :)