Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the purpose of typename assignment inside templates

Tags:

c++

templates

I have come across this piece of code (I'm trying to include all details in case I'm missing something):

template< typename TYPE = TYPE_with_an_arbitrarily_long_name,
          typename KIND = KIND_with_an_arbitrarily_long_name>

class Foo
{
public:
    virtual void bar(TYPE& t, KIND& k) = 0;
};

And the part I don't understand is the assignments inside the template:

template <typename TYPE = TYPE_with_an_arbitrarily_long_name, ..

I have been trying to understand the effect of this but so far I couldn't produce any. Here are some stuff I have tried:

#include <iostream>
#include <typeinfo>
using namespace std;

template<typename T>
void foo(T t) {
    cout << typeid(t).name() << " ";
}

template<typename T = int>
void bar(T t) {
    cout << typeid(t).name() << " ";
}

template<typename T = double>
void baz(T t) {
    cout << typeid(t).name() << " ";
}

int main()
{
    cout << "\nfoo: ";
    foo(3); foo<int>(3); foo<double>(3);
    cout << "\nbar: ";
    bar(3); bar<int>(3); bar<double>(3);
    cout << "\nbaz: ";
    baz(3); baz<int>(3); baz<double>(3);
    return 0;
}

prints out:

foo: i i d
bar: i i d
baz: i i d

So my question is:

  1. What is the effect of assignment inside template?
  2. What is the purpose of using it in the above example?
  3. There is no third question.

Any help is appreciated..

EDIT turned out functions are only compilable with c++11

like image 559
none Avatar asked Nov 26 '12 13:11

none


People also ask

What does template typename do?

" typename " is a keyword in the C++ programming language used when writing templates. It is used for specifying that a dependent name in a template definition or declaration is a type.

Why is typename needed?

The typename keyword is needed whenever a type name depends on a template parameter, (so the compiler can 'know' the semantics of an identifier (type or value) without having a full symbol table at the first pass).

What is the difference between class and typename in template?

There is no difference between using <typename T> OR <class T> ; i.e. it is a convention used by C++ programmers.


2 Answers

This is called 'default template argument' and specifies which type is used, when none is specified - alike default function parameters. This technique is widely used for classes - look at definition of std::vector or std::string, and you will see they have multiple default type parameters.

Best use for default type parameters for function templates is when type argument cannot be easily deduced from actual arguments, and it is not specified explicitly - then compiler will use default one. In your example there is no need for default types, because it can be easily deduced from actual call parameters.

Until C++0x default type parameters were allowed only for class templates - they were not possible to use with function templates. With C++0x it changed, but some older compilers (for example Visual C++ 2008) would not let you to use them.

like image 57
Maciek Avatar answered Sep 21 '22 15:09

Maciek


These are not assignments but rather “default values” for the type arguments of the template, much like there is a similar syntax for default value arguments of functions. They are used when an explicit argument is not specified.

For bar and baz function templates in your example, it makes no sense because for these functions, T will be derived from the specified arguments.

like image 36
Alexey Feldgendler Avatar answered Sep 22 '22 15:09

Alexey Feldgendler