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

  • Home
  • SEARCH
  • 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 6183437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:21:56+00:00 2026-05-24T01:21:56+00:00

I am reading C++ Templates Complete Guide and came across this non-type function template

  • 0

I am reading C++ Templates Complete Guide and came across this non-type function template parameters code (I have added the main() and other parts except the function definition and call):

#include <vector>
#include <algorithm>
#include <iostream>

template <typename T, int value>
T add (T const & element){
  return element + value;
}

int main() {
  int a[] = {1,2,3,4};
  int length = sizeof (a) / sizeof (*a);
  int b[length];
  std::transform (a, a + length, b, (int(*)(int const &))add <int, 5>); //why?
  std::for_each (b, b + length, [](int const & value){ std::cout << value << '\n'; });
  return 0; 
}

I did not understand after reading from the book why we need typecasting of the function call?

EDIT:
Explaination from the book:

add is a function template, and function templates are considered to name a set of overloaded functions (even if the set has only one member). However, according to the current standard, sets of overloaded functions cannot be used for template parameter deduction. Thus, you have to cast to the exact type of the function template argument:…

Compiler: g++ 4.5.1 on Ubuntu 10.10

  • 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-05-24T01:21:57+00:00Added an answer on May 24, 2026 at 1:21 am

    Strictly speaking, you couldn’t refer to the specialization of a function template by simply giving a template argument list. You always had to have a target type around (like, a function parameter type you pass to, or a cast type you cast to, or a variable type you assign to).

    That was the case even if the target type is completely free of template parameters, for example

    template<typename T> void f() { }
    template<typename T> void g(T) { }
    
    int main() {
      g(f<int>); // not strictly valid in C++03
      g((void(*)())f<int>); // valid in C++03
    }
    

    The committee added rules that were adopted into C++0x and by popular compilers in their C++03 mode that made it possible to omit a target type if you supply a complete template argument list supplying types for all template parameters, together with all default template arguments.

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

Sidebar

Related Questions

I am reading the Complete Guide on Templates and it says the following: Where
After reading this article Storing C++ template function definitions in a .CPP file ,
I was reading a book on templates and found the following piece of code:
Reading this question I found this as (note the quotation marks) code to solve
Reading on another forum I've came across the world of CSS Frameworks. The one
Reading over the responses to this question Disadvantages of Test Driven Development? I got
Reading through this question on multi-threaded javascript, I was wondering if there would be
Reading source code of sample projects, such as Beast and Bort, are recommended as
Reading this post has left me wondering; are nightly builds ever better for a
Possible Duplicate: What are the disadvantages of using templates? Reading about templates I found

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.