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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:01:38+00:00 2026-06-17T13:01:38+00:00

I have a function : template<class T> static string format(T ui, T sentinal, char

  • 0

I have a function :

template<class T>
static string
format(T ui, T sentinal, char listSeparator)
{
    stringstream s;
    if (ui == sentinal)
    {
        s << "n/a" << listSeparator;
    }
    else
    {
        s << ui << listSeparator;
    }
    return s.str();
}

The way the function was called is :

output << format(field1,Backend::NA_Value, csvSeparator);
output << format(field2,Backend::NA_Value, csvSeparator);
/// ...etc

Previously field1 and field2 were of the type unsigned int .
It was decided to change these types to be unsigned long long.
But the compilation error occurs :

std::string format(T,T,char)' : template parameter 'T' is ambiguous
main.cpp(39) : see declaration of 'format'
could be 'Juint'
'unsigned __int64'

What is the reason for that?NA_Value?It is defined as :

static const Juint NA_Value = (Juint) -1;
typedef unsigned int   Juint

It can`t determine the template T ?!
Where from compiler decides about __int64?

  • 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-17T13:01:40+00:00Added an answer on June 17, 2026 at 1:01 pm

    This is a problem of template type deduction, where you have the same type occurring multiple times in the signature of a function. The compiler just doesn’t know which of the two types you want T to be, because in the call the two occurrences are of a different type.

    You call format like the following (with Juint being replaced according to your typedef):

    format(unsigned long long ui, unsigned int sentinal, char listSeparator);
    

    You changed the type of the former argument so now it is different from the second. Before you did this change, both parameters had the same type.

    To solve the problem, you have the following options:

    • Make the template types different. (see answer by Joachim Pileborg)

    • Force a specific type by explicitly stating it as a template parameter when you call the function. This will automatically cast the non-matching parameter(s) like you are used to it for normal functions (no type deduction happens in this case):

      output << format<Juint>(field2,Backend::NA_Value, csvSeparator);
                      ^^^^^^^
      
      output << format<unsigned long long>(field2,Backend::NA_Value, csvSeparator);
                      ^^^^^^^^^^^^^^^^^^^^
      
    • Cast one of the parameters to the other type. This makes both parameters be the same and type deduction will succeed:

      output << format(static_cast<Juint>(field2),Backend::NA_Value, csvSeparator);
                       ^^^^^^^^^^^^^^^^^^
      
      output << format(field2,static_cast<unsigned long long>(Backend::NA_Value), csvSeparator);
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      
    • Change the typedef of Juint, too, so the types become the same again and type deduction also succeeds.

    • Add type traits / enable_if which will restrict the template types to specific classes of types fulfilling a condition, but in your case I don’t think that this is what you want.

    Personally, I’d wish there was another option: It would be nice to have the type deduction only perform on the first parameter and force the second parameter to be cast on the call site automatically (like a “T but don’t deduce” parameter type). But there isn’t such an option.

    The best options in your case is to make Juint also of the same type (4th option on the list) or to make your function “more general” by going for the first option. Care has to be taken on the usages of the types within the function, as pointed out in the answer by Joachim Pileborg in general, but in your case it should be fine without changing the function’s body.

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

Sidebar

Related Questions

I have this code: class USerializer { public: template<typename T> static std::string serialize(std::list<T*> listOfObjectToSerialize)
I have a template class with a variadic template member function that I am
Let's assume we have a template function foo: template<class T> void foo(T arg) {
Suppose I have a function which looks like this: template <class In, class In2>
I have the following templated function... template< class T > T *create_object( lua_State *L
Suppose I have a static function template template<int I> void ft() within a struct
I have a static std::map<std::string, CreateGUIFunc> in a class that basically holds strings identifying
If I have one template class and template function like this template <class T>
I have template function compare defined as below. #include<iostream> using namespace std; template<typename T>
Inside my template function I have the following code: TypeName myFunction() { TypeName result;

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.