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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:54:32+00:00 2026-06-15T06:54:32+00:00

In the following : template<typename Type> struct MyClass { template<typename OtherType> MyClass(const MyClass<OtherType>& x);

  • 0

In the following :

template<typename Type>
struct MyClass
{
    template<typename OtherType> MyClass(const MyClass<OtherType>& x);
    template<typename OtherType = Type> void test(const MyClass<OtherType>& x);
};

In the function test what is done between :

Case 1 : The default parameter is priority : the conversion constructor MyClass<Type>(const MyClass<OtherType>& x) is implicitely called and MyClass<Type>::test<Type>(const MyClass<Type>& x) is called.

Case 2 : The deduced parameter is priority : MyClass<Type>::test<Type>(const MyClass<OtherType>& x) is called.


I think that the good answer is the second one, but I’m not sure. Can you confirm me that (and that this situation is well-defined by the standard) ?


EDIT : The test function is called by :

MyClass<double> d;
MyClass<unsigned int> ui;
d.test(ui); // <- So the question is : is ui implicitely 
            //    converted to MyClass<double> or not ?
  • 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-15T06:54:33+00:00Added an answer on June 15, 2026 at 6:54 am

    test will be called as

    MyClass<double>::test(const MyClass<unsigned int> &)
    

    i.e. there will be no conversion of ui from MyClass<unsigned int> to MyClass<double>.

    A default template argument never overrides a given one. It is only used when no template argument is given and the compiler can’t deduce it from the function arguments.

    From the C++11 Standard:

    (§14.8.2/5) The resulting substituted and adjusted function type is used as the type of the function template for template argument deduction. If a template argument has not been deduced, its default template argument, if any, is used. [ Example:

    template <class T, class U = double>
    void f(T t = 0, U u = 0);
    void g() {
      f(1, ’c’);      // f<int,char>(1,’c’)
      f(1);           // f<int,double>(1,0)
      f();            // error: T cannot be deduced
      f<int>();       // f<int,double>(0,0)
      f<int,char>();  // f<int,char>(0,0)
    }
    

    — end example ]

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

Sidebar

Related Questions

The following template specialization code: template<typename T1, typename T2> void spec1() { } Test
I have the following template declaration: template <typename T> void IterTable(int& rIdx, std::vector<double>& rVarVector,
Consider the following function template: template<typename T> void Foo(T) { // ... } Pass-by-value
I'm trying to create a function: template <typename T> void doIt( T*& p )
Consider the following class: class MyClass { public: template<class T> typename T::result_type apply(T& func)
In C++ I can do the following: template<typename T, typename V> struct{ void operator()(T
The following code: template <typename S, typename T> struct foo { void bar(); };
I have the following code: template<typename Parent, typename T, void (Parent::*Setter)(T), T (Parent::*Getter)()> struct
Consider the following program: template <typename T> struct t { struct base { void
Consider the following code: template<size_t head,size_t ... Dims> struct make_vec { typedef typename make_vec<Dims...>::type

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.