Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will static_if deprecate template specialization?

Some usual template specialization like this:

template<class T>
class C
{
    void common() { ... }
    void f2 = delete;
};

template<>
class C<int>
{
    void common() { ... }
    void f1() { ... }
};

Could be represented with static_if as:

template<class T>
class C
{
    void common() { ... }

    static_if(std::is_same<T, int>::value)
    {
        void f1( ) { ... }
    }
    else
    {
        void f2( ) = delete;
    }
}

Are these directly competing features? Can template specialization do something static_if cannot? It seems like static_if can do everything template specialization can do, and much much more.

As an aside: I don't really like static_if in this context because it potentially makes what parts of an interface are available to you in any given circumstance non-obvious. Maybe template specialization still provides clearer syntax in some cases.

like image 389
David Avatar asked Aug 22 '12 20:08

David


People also ask

Are template specializations inline?

An explicit specialization of a function template is inline only if it is declared with the inline specifier (or defined as deleted), it doesn't matter if the primary template is inline.

What is template specialization?

The act of creating a new definition of a function, class, or member of a class from a template declaration and one or more template arguments is called template instantiation. The definition created from a template instantiation is called a specialization.

What is the other name of full specialization?

Explanation: explicit specialization is another name of full specialization.


2 Answers

One thing that static if won't do for you is the "primary" way of using template specializations -- providing generic behaviour in one place and letting the users of your code override (= specialize) it for their specific needs/data types/etc...

like image 118
Grzegorz Herman Avatar answered Sep 23 '22 20:09

Grzegorz Herman


No, static_if will not deprecate explicit template specialization. Explicit template specialization is a more powerful feature than static_if, providing many capabilities static_if isn't intended to. static_if is simply a more convenient and readable way to express certain things.

static_if can't do certain things explicit template specialization can, such as changing the the base classes a class inherits from.

struct S {};

template<typename T>
struct T
  static_if(is_same<T,int>::value) { : S }  // ?
{ };

template<typename T>
struct T {};

template<>
struct T<int> : S {};
like image 29
bames53 Avatar answered Sep 23 '22 20:09

bames53