Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7931885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:51:04+00:00 2026-06-03T20:51:04+00:00

I am trying to learn C++ myself by reading a book and do exercise

  • 0

I am trying to learn C++ myself by reading a book and do exercise from that book.

Now I am trying to declare an instance of function template inside the .cpp file. I know that I can declare/define that function template in header file, but I am still curious about how to do that inside the .cpp file.

Here is piece of code which is trivial but demonstrate my problem:

// temp_func.h
#ifndef GUARD_temp_func
#define GUARD_temp_func

#include <iostream>

using std::cout;    using std::endl;

int addone(int);
int addtwo(int);

template<typename F>
void call_adds(int, F);

#endif

Header file

// temp_func.cpp
#include "temp_func.h"

using std::cout;    using  std::endl;

int addone(int n)
{
    return n + 1;
}

int addtwo(int n)
{
    return n + 2;
}

template<typename F>
void call_adds(int n, F f)
{
    cout << f(n) << endl;
}
template void call_adds<addone>(int n, F addone);

.cpp file, and obviously the last line doesn’t work.

Edit:
Based on solutions provided by n.m.

template<int F(int)>
void call_adds(int n)
{
    cout << F(n) << endl;
}
template void call_adds<addtwo>(int);
template void call_adds<addone>(int);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T20:51:05+00:00Added an answer on June 3, 2026 at 8:51 pm

    Your call_ads template requires a type parameter. addone is not a type, it’s a function. You can specialize call_adds for a type like int(int), but not for individual functions of that type.

    You can create a function template with a non-type template parameter:

    template <int F(int)>
    void call_adds(int n)
    {
        cout << F(n) << endl;
    }
    

    and specialize it:

    template<> void call_adds<addtwo> (int n) { ... }
    

    Note that call_adds doesn’t have a regular function parameter any more, only a template parameter.

    The compiler doesn’t really care whether you declare something in a header file or in a source file, as long as the declaration is visible where is should be.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn programming by myself, I'm working from a book that has
I'm trying to teach myself python right now, and I'm using exercises from Learn
In trying to learn JavaScript closures, I've confused myself a bit. From what I've
I'm trying to learn PHP, and I figured I'd make myself a simple exercise
I'm trying to learn myself some C++ from scratch at the moment. I'm well-versed
I am trying to learn using databases correctly by myself and now I realised,
I have been trying to learn from reading tutorials online and stuff but I
I am trying to learn assembly my self, and I have been reading different
I´m trying to learn C#, coming from a Python/PHP background, and I´m trying to
I'm trying to learn more about WPF. I ran through an online tutorial that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.