20

I have two libraries that I work with and I wrote a converter between some of the types/structs they use, for convenience.

template<typename T>
struct unsupportedType : std::false_type
{};

template<typename T>
FormatB getFormat()
{
    static_assert(
        unsupportedType<T>::value, "This is not supported!");
}

template<>
FormatB getFormat<FormatA::type1>()
{
    return FormatB(//some parameters);
}

template<>
FormatB getFormat<FormatA::type2>()
{
    return FormatB(//some other parameters);
}

Now due to the unsupportedType struct, the compiler does not immediately see that the assertion will always fail and thus does not throw a compilation error if the non-specialized version is not called somewhere. However, the compiler therefore also does not know that a return statement after the static_assert is unnecessary. I do not just want to place an arbitrary return statement after the assert to get rid of the warning.

Question: What is a clean way to get rid of the warning?

3
  • 1
    Have you tried the C++ attribute [[noreturn]] on the function declaration? Commented Nov 18, 2016 at 16:07
  • 1
    What's wrong with just putting return after static assert? Commented Nov 18, 2016 at 16:39
  • 1
    What is the expected behaviour when the static_assert fails? The reason the compiler warns is that there are ways to break the static_assert (specializing the unsupportedType struct).
    – NOhs
    Commented Nov 18, 2016 at 16:44

4 Answers 4

20

I would try to avoid the static_assert, by using something like

template<typename T> FormatB getFormat()=delete;

The compiler writers can then work on improving those error messages.

3
  • 1
    This actually does not answer the question directly, but it gives the best answer to my actual problem, I guess, how to not allow the non specialized version to be called. I don't know if I should now reformulate the question, or how to proceed to make this question + answer the most useful to everyone.
    – NOhs
    Commented Nov 18, 2016 at 16:50
  • 1
    Somehow I missed the elephant in the room.
    – Quentin
    Commented Nov 18, 2016 at 18:20
  • While this is legal code, at least one compiler (clang) will reject it.
    – Sam
    Commented Nov 18, 2016 at 22:19
6

There's more than one way to exit a function, returning is one, but throwing is another:

template<typename T>
FormatB getFormat()
{
    static_assert(
        unsupportedType<T>::value, "This is not supported!");
    throw 0; // Unreachable
}
9
  • Is it considered good practice to just throw 0? I could imagine that if for some reason somebody is "clever" enough to specialize unsupportedType giving it a true value, it might be hard to track if it just throws an arbitrary 0.
    – NOhs
    Commented Nov 18, 2016 at 16:22
  • @MrZ that throw is unreachable, it'll never actually happen. It's there to make the compiler think that the function always finished execution in well-defined manner (because for the compiler the static_assert is not guaranteed to fire).
    – krzaq
    Commented Nov 18, 2016 at 16:23
  • 2
    @MrZ then I suggest throw std::runtime_error{"Why the hell are you trying to break my TMP?"}; ;)
    – Quentin
    Commented Nov 18, 2016 at 16:24
  • 4
    @MrZ in the words of...someone, "Guard against Murphy, not Machiavelli" Commented Nov 18, 2016 at 16:24
  • 3
    @MrZ I know you've decided on the other answer, but future visitors here might want a logic_error instead: succeeding in calling a function that should issue a compile error indicates an error in the program logic, after all Commented Nov 18, 2016 at 16:51
6

For you said in a comment to the accepted answer that it actually does not answer the question directly, here is a working solution that can be of help for future readers.


To solve the issue you can just define your function template as it follows:

template<typename T, typename R = void>
R getFormat()
{
    static_assert(
        unsupportedType<T>::value, "This is not supported!");
}

No changes are required for the other specializations, the type R can be deduced directly from the return type.


It follows a minimal, working example that shows how it works:

#include<type_traits>

template<typename T>
struct unsupportedType : std::false_type
{};

struct FormatA{
    using type1 = int;
    using type2 = char;
};

struct FormatB{};

template<typename T, typename R = void>
R getFormat()
{
    static_assert(
        unsupportedType<T>::value, "This is not supported!");
}

template<>
FormatB getFormat<FormatA::type1>()
{
    return FormatB();
}

template<>
FormatB getFormat<FormatA::type2>()
{
    return FormatB();
}

int main() {}
4

You can use GCC's __builtin_unreachable (it's way down on that link) to communicate to the compiler that a given line is unreachable, which will suppress that warning. (I mostly mention this for C devs who end up here; for C++, the method using delete above is better).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.