1

I wish to write a function template to find larger of the two numbers passed in as parameters. And I want to explicitly instantiate this function template for int.

So I wrote in the larger.h the following;

template <typename T>
const T& larger(T a,T b);

I wrote in larger.cpp the following:

#include<iostream>
#include"larger.h"
const T& larger(T a,T b) {
    return a<b?b:a;
}

In the main.cpp:

#include<iostream>
#include "larger.h"
int main(){
    template int()
    const& larger<int>(int,int);
    std::cout << larger(6,8) << std::endl;
}

When I compile and try to run main.cpp,I get the following error:

error: expected primary-expression before ‘template’
 template int()

I am unable to proceed further. What's wrong?

Barry
  • 286,269
  • 29
  • 621
  • 977
Aditya
  • 21
  • 3
  • 1
    What's wrong with [std::max](http://en.cppreference.com/w/cpp/algorithm/max)? It already exists and does exactly what you want. – Andrew Sep 28 '15 at 17:40
  • 1
    What is `template int()` supposed to be? – huu Sep 28 '15 at 17:44

2 Answers2

4

Problems I see:

  1. templates need to be implemented in header files, not source files. See Why can templates only be implemented in the header file?.

  2. The syntax for declaring an explicit specialization of larger for int is:

    template <> int const& larger<int>(int,int);
    

    That is different than the syntax for explicit instantiation of larger for int. That will be:

    template int const& larger<int>(int,int);
    

    It's not clear from your post which one you want.

  3. The declaration must be outside a function, not inside.

    template <> int const& larger<int>(int,int);
    
    int main()
    {
       std::cout<<larger(6,8)<<std::endl;
       return 0;
    }
    
  4. larger returns a reference to a temporary. Change it to return a value

    template <typename T>
    T larger(T a,T b);
    

    or change the arguments to be of type T const&.

    template <typename T>
    T const& larger(T const& a, T const& b);
    
Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 2
    @RSahu "The lifetime of a temporary bound to the returned value in a function return statement (6.6.3) is not extended; the temporary is destroyed at the end of the full-expression in the return statement." – Barry Sep 28 '15 at 17:57
  • Thanks for the clarification, @Barry. – R Sahu Sep 28 '15 at 18:06
  • No problem. Those rules are really complicated... easy to get confused in the mess. – Barry Sep 28 '15 at 18:12
4

There are several problems with your code.

  1. Your larger is returning a reference to a temporary. You probably want to take your arguments by reference as well:

    template <typename T>
    T const& larger(T const&, T const& );
    
  2. The syntax for explicitly instantiating a function template is:

    template int const& larger<int>(int const&, int const&);
    

    and it cannot be within the body of a function.

  3. Explicit instantiations require the definition to be visible, so it can't be in main.cpp, it must go in larger.cpp. That is:

    #include"larger.h"
    
    const T& larger(T const& a,T const& b) {
        return a<b?b:a;
    }
    
    // must go here
    template int const& larger<int>(int const&, int const&);
    
  4. Use std::max.

Barry
  • 286,269
  • 29
  • 621
  • 977