maciekgajewski/Fast-Log

compile error

Closed this issue · 2 comments

logger.h:175:47: error: ‘str’ is not a constant expression

can you help me?

I write a simple test below, compile failed at log2("Hello", 42): error: ‘str’ is not a constant expression

#include <iostream>

constexpr size_t CountPlaceholders(const char* formatString) {
    return (formatString[0] == '\0') ? 0
                                     : ((formatString[0] == '%' ? 1u : 0u) +
                                        CountPlaceholders(formatString + 1));
}

template <typename... Args>
constexpr int sizeofArgs(Args&&...) {
    return sizeof...(Args);
}

#define LOG(fmt, ...) static_assert(CountPlaceholders(fmt) == sizeofArgs(__VA_ARGS__))

void log1(const char* str, int number) {
    LOG("str=%, number=%", str, number);
}

void log2(const std::string& str, int number) {
    LOG("str=%, number=%", str, number);
}

int main(int argc, char* argv[]) {
    LOG("%d %d", argc, argv);
    log1("Hello", 42);
    log2("Hello", 42);
    return 0;
}

Try different way of counting arguments, for instance:

#define LOG(fmt, ...) static_assert(2 == std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value);

Try different way of counting arguments, for instance:

#define LOG(fmt, ...) static_assert(2 == std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value);

Thank you very much! You are C++ guru!