4

I have a basic class looking something like this:

template <typename TCode, TCode SuccessVal>
class Error
{
public:
    typedef TCode code_t;

    Error(TCode code, char const *descr="Unknown Error."):
        code(code), descr(descr)
    {

    }

    ...

    char const *description() const
    {
        return descr;
    }

    ...

private:
    TCode code;
    char const *descr;
};

All it does is encapsulate some kind of error code enum class so that it provides a bit more context for logging.

Now say I have a function panic:

template <typename TCode, TCode SuccessVal>
void panic(Error<TCode, SuccessVal> const &e)
{
    puts("Panic!");
    printf("Unrecoverable error: %s", e.description());
    abort();
}

Compiling with -fdump-tree-original shows that in my case this results in a couple of distinct functions, with the exact same code. This is what you would expect, but probably not what you would desire.

An obvious route would be a base class that just had the message and a constructor taking the message, but I find that rather unattractive.

We don't ever use the error code itself so nothing we do depends on T. How can I avoid a ton of template instantiations that compile to basically the same code?

Another desirable feature would be to make sure that whatever type TCode is, it is coercible to an integer type.

2
  • 2
    In Visual Studio and other toolchains, the linker detects these and merges them. Aug 4, 2014 at 22:47
  • @MooingDuck: That's a good point!
    – Skurmedel
    Aug 5, 2014 at 19:45

2 Answers 2

6

An obvious factorization of this code would be this:

[[noreturn]] void panic_with_message(char const * msg)
{
    std::printf("Panic!\nUnrecoverable error: %s\n", msg);
    std::fflush(stdout);
    std::abort();
}

template <typename T, T Val>
[[noreturn]] static inline void panic(Error<T, Val> e)
{
    panic_with_message(e.description());
}

You could put only the template into a header, along with a declaration of the function, and keep the function definition in a separate translation unit. That should keep the code bloat to a minimum:

// panic.hpp

#ifndef H_PANIC
#define H_PANIC

#include "error.hpp"  // for Error<T, Val>

[[noreturn]] void panic_with_message(char const * msg);

template <typename T, T Val>
[[noreturn]] static inline void panic(Error<T, Val> e)
{
    panic_with_message(e.description());
}

#endif
6
  • 3
    I admit to commonly making a <void> specialization of my templates and abusing it for this purpose :( Aug 4, 2014 at 22:48
  • neither visual c++ 12.0 (2013) nor g++ 4.8.2 support [[noreturn]]. both compilers have pre-C++11 language extensions that do the same, respectively __declspec( noreturn ) and __attribute__((noreturn)), IIRC. so in order to present examples that the users can try out, at this time I think it's better to capture the intended semantics by using a macro symbol, perhaps with a suggested implementation (or explanation). Aug 4, 2014 at 22:56
  • 2
    @Cheersandhth.-Alf Are you sure that g++ 4.8.2 doesn't support [[noreturn]]? It does compile at least in 4.8.1 and This suggests that generalized attributes were implemented in 4.8: gcc.gnu.org/bugzilla/show_bug.cgi?id=53528
    – eerorika
    Aug 4, 2014 at 23:16
  • @user2079303: thanks. now, since it compiles, i'm less sure. ;-) Aug 4, 2014 at 23:33
  • @Kerrek SB: Hmm, that's somewhat in line with what I had in mind, although I would've hoped there was some neat template magic available. I am unfamiliar with [[noreturn]], I guess it means the same thing as _No_return in C11?
    – Skurmedel
    Aug 7, 2014 at 20:07
1

Another option is to use extern template declarations. c++11 compilers and older visual studio versions support this (as a language extension.) Assuming you have a reasonably small number of Error instantiations, you can define an extern template declaration of panic for each one and place the actual function instantiation in a single translation unit. This ensures you only have one instantiation for each variant of panic.

panic.hpp

#ifndef PANIC_HPP
#define PANIC_HPP

template <typename TCode, TCode SuccessVal>
struct Error
{
    const char* description() { return "boom!"; }
};

template <typename TCode, TCode SuccessVal>
void panic(Error<TCode, SuccessVal> e);

//! So assume you have Error<ErrorCode, SuccessVal>
//! Place this in the header where the panic function prototype is declared.
//! You'll need one for each type or Error<T,Val> instantiation.
extern template void panic<int, 0>(Error<int, 0> e);
extern template void panic<int, 1>(Error<int, 1> e);
extern template void panic<int, 2>(Error<int, 2> e);

#endif//PANIC_HPP

panic.ipp

#ifndef PANIC_IPP
#define PANIC_IPP

#include "panic.hpp"
#include <cstdio>
#include <cstdlib>

template <typename TCode, TCode SuccessVal>
inline void panic(Error<TCode, SuccessVal> e)
{
    printf("Unrecoverable error: %s scode: %d\n", e.description(), SuccessVal);
    std::abort();
}

#endif//PANIC_IPP

panic.cpp

#include "panic.ipp"

//! Declare the instantiation for each Error<T, Val> which you don't want duplicated.
template void panic<int, 0>(Error<int, 0> e);
template void panic<int, 1>(Error<int, 1> e);
template void panic<int, 2>(Error<int, 2> e);

main.cpp

#include "panic.hpp"

int main()
{
    panic(Error<int, 0>());
    panic(Error<int, 1>());//! These compile/link but aren't called due to abort.
    panic(Error<int, 2>());//! ...
    //panic(Error<int, 3>()); //! won't link due to unresolved symbol as not instantiated.
    return 0;
}
1
  • Note this can be combined with Kerrek's answer for an even better solution. Aug 5, 2014 at 0:10

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.