mardi 16 août 2016

Will a compiler discard a class implementation if not used in code?

Probably the title is a bit misleading. But before I get to the actual question, let me explain the situation that I'm currently in.

I need to create several specializations of a class/structure to help me select the proper type depending on the specified template parameter. Here's a simplified example:

struct TypeA;
struct TypeB;
struct TypeC;

template < typename T > struct TypeSelector
{
    typedef DefaultType Type;
};

template <> struct TypeSelector< TypeA >
{
    typedef SuggestedForTypeA Type;
};

template <> struct TypeSelector< TypeB >
{
    typedef SuggestedForTypeB Type;
};

template <> struct TypeSelector< TypeC >
{
    typedef SuggestedForTypeC Type;
};

And I may end up doing several of these specializations that could even be nested.

At first, I looked if I could avoid such approach with the new c++11 keyword using (out of curiosity). But apparently, it can't be used to create specializations. So I'm left with this approach.

However, to my understanding, abusing this type of approach is not very good for the compiled code. As I end up creating a bunch of new types for every other types that I might be using with these specializations.

Ad basically, I'm just using these types as a smarter typedef and not actually creating instances of them.

So my question is. Will a decent compiler see that I'm not using these types and discard them from the compiled code?

Or is there an attribute that I can specify to indicate such behavior?

Recent C++11/14/17 features are not an issue as I'm using MinGW/GCC 6.1.0

Aucun commentaire:

Enregistrer un commentaire