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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:40:27+00:00 2026-06-11T17:40:27+00:00

Possible Duplicate: Template deduction for function based on its return type? I am trying

  • 0

Possible Duplicate:
Template deduction for function based on its return type?

I am trying to figure out how C++ templates work. As an example, I want to write a simple data structure, triple which behaves like STL’s pair but has three items. Of course, I want to implement it using templates. And I am facing a strange behaviour from my point of view.

First, I have a working example. Here is the C++ code that works:

template<typename T1, typename T2>
T1 getSum(T1 a1, T2 a2) {
    return a1 + a2;
}

int main(int argc, int *argv[]) {
    long l1 = 1, l2 = 0, l3 = 0, l4 =0;
    short s1 = 2, s2 = 0, s3 = 0, s4 = 0;
    l2 = getSum(l1, s1);
    l3 = getSum(s1, l1);
    l4 = getSum(1, 2);
    s2 = getSum(l1, s1);
    s3 = getSum(s1, l1);
    s4 = getSum(1, 2);
    cout << l2 << " " << l3 << " " << l4 << endl;
    cout << s2 << " " << s3 << " " << s4 << endl;
    return 0;
}

The result of this program is two lines containing 3 3 3 each. Since I call getSum function with various combination of arguments: (long, short), (short, long) and (integer literal, integer literal) and the return value is assigned into both long and short variables each time, I assume that compiler knows how to make an appropriate conversion or guessing of types by return values (in case of integer literals) each time.

And here we come back to simple triple implementation. I do everything in pretty the same way (as it seems to me) but I get a conversion error. Here is what I mean. My code looks like the following:

template <typename T1, typename T2, typename T3>
struct triple {
    T1 first;
    T2 second;
    T3 third;
};

template <typename T1, typename T2, typename T3>
triple<T1, T2, T3> mt(T1 f, T2 s, T3 t) {
    triple<T1, T2, T3> tr;
    tr.first = f;
    tr.second = s;
    tr.third = t;
    return tr;
};

int main(int argc, char* argv[]) {
    triple<char, char, unsigned short> t = mt(0, 0, 65000);
    cout << t.first << " " << t.second << " " << t.third << endl;
    return 0;
}

However, when I try to compile it, I get the following error:

22: error: conversion from 'triple<int, int, int>' to non-scalar type
'triple<char, char, short unsigned int>' requested

(22nd line is the first line of main function).

It is not surprise that conversion between two aforementioned triples is not a compiler’s business. But it is a surprise for me that compiler cannot determine that since I want to get triple<char, char, short unsigned int> as a result of mt function, template arguments should be determined as T1 == char, T2 == char, T3 == unsigned short.

And since things work this way now, I’m confused about how make_pair function works. Its code looks very alike to mine (again, as it seems to me).

So, my question is: please, clarify the essential differences between my code and working one 🙂

  • 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-11T17:40:28+00:00Added an answer on June 11, 2026 at 5:40 pm

    The problem in your case is that your function mt has a fixed return type already: Triple<int, int, int> (determined by the parameters, and integer constants are always considered to be int unless specified otherwise). Once created that way, it can’t be assigned, since it’s a different type, and there is no type conversion or constructor defined which would allow what you’re trying to do.

    At the moment, there’s no constructor at all explicitly defined in your triple strucht – which means there’s only default constructor and the copy constructor (which works only for the exact same type)!

    But there is a solution:Take a look at the definition of pair: See the copy constructor (which is templated again, in addition to the template parameters on the base class). To make it work for your case, add your own templated copy constructor:

    template <typename T1, typename T2, typename T3>
    struct triple {
        template <typename U1, typename U2, typename U3>    
        triple(U1 f, U2 s, U3 t):
            first(f),
            second(s),
            third(t) {}
        T1 first;
        T2 second;
        T3 third;
    };
    

    Instead of using a function returning a triple, you could also simply use another constructor taking the three parameters:

    template <typename T1, typename T2, typename T3>
    struct triple {
        triple(T1 f, T2 s, T3 t):
            first(f),
            second(s),
            third(t) {}
        T1 first;
        T2 second;
        T3 third;
    };
    

    and then in main you could write:

    triple<char, char, unsigned short> t(0, 0, 65000);
    

    It’s also less to write!

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

Sidebar

Related Questions

Possible Duplicate: trailing return type using decltype with a variadic template function I want
Possible Duplicate: trailing return type using decltype with a variadic template function I'm getting
Possible Duplicate: Templates: template function not playing well with class’s template member function template
Possible Duplicate: Determine if Type is a pointer in a template function I am
Possible Duplicate: How to use enable_if to enable member functions based on template parameter
Possible Duplicate: Dirt-simple PHP templates… can this work without eval ? Let's say I
Possible Duplicate: Possible for C++ template to check for a function’s existence? I am
Possible Duplicate: “Undefined reference to” template class constructor I've just started using templates, and
Possible Duplicate: Destructors of builtin types (int, char etc..) Template Function: template<typename T> void
Possible Duplicate: What does template <unsigned int N> mean? Hi ! Are non-type template

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.