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

  • Home
  • SEARCH
  • 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 724531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:14:00+00:00 2026-05-14T06:14:00+00:00

Is there any way I can construct an new object from the given object

  • 0

Is there any way I can construct an new object from the given object if the template parameters of both objects are identical at run-time? For example:

I have a template class with the declaration:

template<typename _Type1, typename _Type2> class Object;

Next, I have two instantiations of the template:

template class Object<char, int>;
template class Object<wchar_t, wint_t>;

Now, I want to write a member function such as:

template<typename _Type1, typename _Type2>
Object<char, int> Object<_Type1, _Type2>::toCharObject() {
    if(__gnu_cxx::__are_same<_Type1, char>::__value)
        return *this;
    else {
        //Perform some kind of conversion and return an Object<char, int>
    }
}

I have tried a couple of techniques, such as using __gnu_cxx::__enable_if<__gnu_cxx::__are_same<_Type1, char>::__value, _Type1>::__type in a copy constructor for the Oject class, but I keep running into the error:

error: conversion from ‘Object<wchar_t, wint_t>’ to non-scalar type ‘Object<char, int>’ requested

Is there no way I can do this? Any help will be greatly appreciated!

  • 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-14T06:14:01+00:00Added an answer on May 14, 2026 at 6:14 am

    What you have should work, the problem is that the compiler is doing a type check on the return *this part, even if the types aren’t equal (hence the compile error). Just use return (Object<char, int>)(*this); and you should be fine — the only time that code will be executed is when the types are the same anyway, so the cast does nothing other than working around the compile error.

    Alternatively, you can use template specialisation:

    template <class _Type1, class _Type2>
    Object<char, int> toCharObject(Object<_Type1, _Type2> obj)
    {
      // Do conversion and return
    }
    
    // Specialisation when types are equal
    template <>
    Object<char, int> toCharObject(Object<char, int> obj)
    {
      return obj;
    }
    

    This is a free function as you can see. You can do it as a member function, but it’s more tricky because you can’t specialise individual member function — you have to specialise the whole class. You can get around that by factoring out the non-specialised code, but that’s really ugly, and this works just as well.

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

Sidebar

Ask A Question

Stats

  • Questions 413k
  • Answers 413k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I found the solution and thought I'd post back here… May 15, 2026 at 8:22 am
  • Editorial Team
    Editorial Team added an answer It seems that command's Name is the only "documentation" available… May 15, 2026 at 8:22 am
  • Editorial Team
    Editorial Team added an answer I still have not found a real solution to this… May 15, 2026 at 8:22 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.