Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial template function specification in C++ works, but why?

I'm trying to find out whether partial specification of templated functions is part of the C++ standard, or whether this is something compiler specific.

By partial specification, I mean specifying only the types the compiler can't deduce. So if I have a template function 'f' that takes 3 types, and one is used in a parameter and can be deduced, I might call 'f' with the form f<type, type>(parameter)

Here's an example:

#include <iostream>
#include <tuple>
#include <string>

template<class A, class B, class C>
std::tuple<A, B> test(C c)
{
    // do something based on c, return tuple with types A and B
    return std::make_tuple(A(), B());
}

int main(void)
{
    // I expected I would have to use this form.  Specify all parameters.
    std::tuple<int, int> value3 = test<int, int, int>(5);

    // Here, I only specified the return value types, did not specify the parameter type, yet it compiles.
    auto value1 = test<int, int>("c-string");

    // Similar example here.  Return types specified, parameter type deduced.  Compiles fine.
    auto value2 = test<std::string, int>(42);

    return 0;
}

I've tested this with g++ 4.5.3, g++ 4.6.3, VS2010 and VS2012. Since it seems to be widely supported by the compilers, I'm betting it is part of the standard, but can anyone confirm that? Does anyone have any links or pointers to resources that might explain why this works?

like image 832
James Freeman Avatar asked Nov 25 '12 19:11

James Freeman


People also ask

Which function is useful when template of template is used?

Explanation: As a template feature allows you to write generic programs. therefore a template function works with any type of data whereas normal function works with the specific types mentioned while writing a program.

What is the purpose of template parameter?

A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.

When we specialize a function template it is called?

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 function template explain this concept with the help of an example?

Defining a Function Template In the above code, T is a template argument that accepts different data types ( int , float , etc.), and typename is a keyword. When an argument of a data type is passed to functionName() , the compiler generates a new version of functionName() for the given data type.


1 Answers

Paragraph 14.8.1.2 of the C++03 standard states

"Trailing template arguments that can be deduced (14.8.2) may be omitted from the list of explicit template-arguments."

like image 166
Vaughn Cato Avatar answered Oct 12 '22 23:10

Vaughn Cato