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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:51:50+00:00 2026-06-04T12:51:50+00:00

How do I implement optional template parameters? I’d like to have a class MyStruct<T1,T2,T3>

  • 0

How do I implement optional template parameters?

I’d like to have a class MyStruct<T1,T2,T3>, where it’s allowed to use only the first or the first two params. Now, the functions that process MyStruct<T1,T2,T3> should also somehow deal correctly with the unused template params.

Example:

#include <iostream>

template<class T1, class T2, class T3>
struct MyStruct {
  T1 t1; T2 t2; T3 t3;
  MyStruct() {}
  MyStruct(T1 const& t1_, T2 const& t2_, T3 const& t3_)
    : t1(t1_), t2(t2_), t3(t3_) {}
};

template<class T1, class T2, class T3>
MyStruct<T1, T2, T3> myplus(MyStruct<T1, T2, T3> const& x,
                MyStruct<T1, T2, T3> const& y) {
  return MyStruct<T1, T2, T3>(x.t1 + y.t1, x.t2 + y.t2, x.t3 + y.t3);
}

int main() {
  typedef MyStruct<int, double, std::string> Struct;
  Struct x(2, 5.6, "bar");
  Struct y(6, 4.1, "foo");
  Struct result = myplus(x, y);
  // (8, 9.7, "barfoo")
  std::cout << result.t1 << "," << result.t2 << "," << result.t3;
}

Now I’d like to change the code so that the above main() function still works, but the following would also work:

typedef MyStruct<std::string, int> Struct;
// result: ("barfoo", 5)
Struct result = myplus(Struct("bar", 2), Struct("foo", 3));

Or this:

typedef MyStruct<int> Struct;
// result: (5)
Struct result = myplus(Struct(2), Struct(3));

I think boost::tuple uses a similar trick, where you may use boost::tuple<A>, boost::tuple<A,B>, boost::tuple<A,B,C>, but I’m not sure how they do it.

  • 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-04T12:51:51+00:00Added an answer on June 4, 2026 at 12:51 pm

    If I get you correctly you should be able to pass default parameters for your template:

    template<class T1, class T2 = Default, class T3 = Default>
    

    Where you can substitue any type for the Default.

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

Sidebar

Related Questions

I'm trying to implement optional parameters in a stored procedure that I have but
I would like to implement single sign-on (sso) in a management application we have
I have a controller with an Index method that has several optional parameters for
I have UI where a user can search on optional parameters.I am writing a
I have a DateInterval class that is annotated with @Embeddable and it has two
What is the standard way to implement optional query parameters in a .NET WinForms
In ActionScript 3 you can declare optional parameters like this: function (i:int = 0,
I have a base class with an optional virtual function class Base { virtual
I'm trying to implement a service with Multiple Optional Parameters using ServiceStack.Net At the
In C++ methods can have optional arguments, like this: void myFunction (int arg1, int

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.