Search code examples
c++c++11g++variadic-templatesvariadic-functions

Why is this type_traits code giving me an integer-to-pointer cast warning?


Without going into too much detail, I've created a variadic template function which does different things depending on the type of its template parameters. I simplified the implementation into a really simple console print example:

#include <cstdint>
#include <cstddef>
#include <cstdio>
#include <cstring>
#include <type_traits>

void print(const char *chars) {
    printf("%s", chars);
}

template<typename FirstType, typename ... OtherTypes>
inline void print(FirstType first, OtherTypes... others);

template<typename ... OtherTypes>
inline void print(const char *chars, OtherTypes... others) {
    print(chars);
    print(" ");

    if (sizeof...(others) == 0) {
        print("\r\n");
    }
    else {
        print(others...);
    }
}

template<typename FirstType, typename ... OtherTypes>
inline void print(FirstType first, OtherTypes... others) {
    char buffer[10];
    const char *format = nullptr;

    if (std::is_same<int, FirstType>::value) {
        format = "%d";
    }
    else if (std::is_same<char, FirstType>::value) {
        format = "%c";
    }
    else if (std::is_pointer<FirstType>::value && (std::is_same<char*, FirstType>::value || std::is_same<const char*, FirstType>::value)) {
        print((const char *) first, others...);
        return;
    }

    if (format != nullptr) {
        snprintf(buffer, 10, format, first);
    }

    print((const char *) buffer, others...);
}

int main() {
    print("this is an example:", 'X');

    return 0;
}

In the above code I call the variadic function with a const char* and a char. It works correctly, however the trouble is that the compiler gives me a warning:

[Timur@Timur-Zenbook tm]$ g++ tm.cpp -Wall -Wextra -o tm
tm.cpp: In instantiation of ‘void print(FirstType, OtherTypes ...) [with FirstType = char; OtherTypes = {}]’:
tm.cpp:24:14:   required from ‘void print(const char*, OtherTypes ...) [with OtherTypes = {char}]’
tm.cpp:52:37:   required from here
tm.cpp:40:15: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
         print((const char *) first, others...);
               ^~~~~~~~~~~~~~~~~~~~

The warning comes for the char argument, as if the function incorrectly determined the type of char to be the same as const char* and therefore the code path for const char* were triggered.

Why am I getting this warning and how do I fix it?


Solution

  • Why am I getting this warning

    The code in that branch is still instantiated when FirstType is char, even though that branch will never actually be executed.

    how do I fix it?

    Move code that only makes sense for some type(s) out to an overload or specialized trait, so that it isn't compiled in the first place for non-matching types.

    The simplest change is to remove the else if(std::is_pointer... branch entirely, and add this overload, which has the same effect:

    template<typename ... OtherTypes>
    inline void print(char *chars, OtherTypes... others) {
        print((const char *)chars, others...);
    }