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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T07:01:13+00:00 2026-05-17T07:01:13+00:00

In this code: template<class T> struct Side { }; template<class T> struct LeftSide :

  • 0

In this code:

template<class T>
struct Side
{
};

template<class T>
struct LeftSide : public Side<T>
{
};
template<class T>
struct RightSide : public Side<T>
{
};

Side<int>* f(int left, int right)
{
    return left < right ? new LeftSide<int> : new RightSide<int>;//<---Here I'm returning either left or right side
}

int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}

I’m getting an error:
_Error 1 error C2446: ‘:’ : no conversion from ‘RightSide *’ to ‘LeftSide *’_

I’ve thought (wrongly as I see) that I can assign pointer from derived to base without any problems. So where is the problem?

  • 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-05-17T07:01:13+00:00Added an answer on May 17, 2026 at 7:01 am

    The problem is not with the conversion from either LeftSide or RightSide to Side<T>. As you originally thought, this conversion would be fine.

    Rather, the problem is with this expression:

    left < right ? new LeftSide<int> : new RightSide<int>
    

    Let’s break this down a little. The ternary operator (properly referred to in the Standard as the ‘comparison operator’) looks like this:

    bool_val ? lhs_expression : rhs_expression
    

    Keep in mind that this whole construct is itself an expression. Meaning it returns a value, which has to have a type, obv. The type of the whole expression is deduced from the types of lhs_expression and rhs_expression. In this case, you have a LeftSide and a RightSide. So, here’s your problem.

    LeftSide and RightSide are not directly related to each other other than having a common base class, and there’s no conversion available between them. (You’d have to write one.) So there’s no single datatype that the bool_val ? lhs_expression : rhs_expression can have. You might think, “well, silly compiler, why not just figure out the common base class and use that?” This is, indeed, a bit of a pain. Leaving the argument of it being Right or Wrong aside, it just doesn’t work that way.

    You have two options.

    One, use a simpler construct:

    if( left < right )
      return new LeftSide<int>;
    else
      return new RightSide<int>;
    

    Two, if you really really want to use the ternary operator (which is the case sometimes), you need to spoon-feed the compiler it’s datatypes:

    Side<int>* f(int left, int right)
    {
        return left < right ? static_cast<Side<int>*>(new LeftSide<int>) : static_cast<Side<int>*>(new RightSide<int>);// now you're good
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: struct A{}; template<class T = A> struct B { void
I have this code inside a header (edited): template <int i> class A {};
I wrote this little code std::map<int,template<class T>> map_; map_.insert(make_pair<int,message>(myMsg.id,myMsg)); but the compiler doesn't seem
I have code that looks like this: template<class T> class list { public: class
the template code is like this: template <class type1> struct DefaultInstanceCreator { type1 *
struct X{}; template<class T> decltype(X() == int()) f(T const&){ return true; } int main(void)
Having this code: template<class ...Args> struct Are_Same { enum {value = Are_Same<Args...>::value}; }; template<class
Consider this code: template <int N> struct X { friend void f(X *) {}
I'm new to C++. Here is the code: template <class T> typename lw_slist {
I have non-template class with a templatized constructor. This code compiles for me. But

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.