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 8886515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:34:38+00:00 2026-06-14T21:34:38+00:00

I have come across this piece of code (I’m trying to include all details

  • 0

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

  • 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-14T21:34:39+00:00Added an answer on June 14, 2026 at 9:34 pm

    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.

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

Sidebar

Related Questions

I'm trying to figure out someone else's code and have come across this piece
I have come across this line of legacy code, which I am trying to
I have just come across this code: function test(){ //... if ( $profilerule ==
I have a come across a piece of code to pop to a specific
I've recently come across this piece of JavaScript code: if (,>=,<=,<>,.indexOf(, + sCompOp +
I have come across this code snippet in a source code #if SILVERLIGHT internal
I have come across this piece of 'voodoo' SQL which is being used to
I have come across this Cocoa application (source code) that shows a main Window.
I have come across this PHP code to check email address using SMTP without
I have come across this great function/command . Colour to RGB, you can do

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.