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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:49:09+00:00 2026-05-27T06:49:09+00:00

I want a templated class with a templated conversion operator and a default constructor,

  • 0

I want a templated class with a templated conversion operator and a default constructor, but my initial attempt isn’t working.

template<typename T>
class C
{
public:
    C() {}

    template<typename U>
    operator U() 
    {
        C<U> c; // (*)
        c._a = dynamic_cast<U*>(_a); 
        return c;
    }
private:
    T* _a;
};

But now, when I try to create an instance of C,

template<typename T>
void F()
{
    ...
    C<T> obj;
}

operator U() keeps calling itself over and over at (*), eventually segfaulting. The same thing happens when I define the function that does the casting externally and call it from operator U()–in which case there is no call to C::C() from within the class definition.

It seems to me then that the conversion operator is getting called when I want the default constructor called–it is essentially trying to convert itself. But surely, there’s a way to do what I’m trying to do?

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

    Note that in operator X (where X is a type) functions, you should usually return something of type X. You’re returning a C<U> when you’re trying to convert the invoking object to a U which causes the following to happen:

    1. int a = someC; (where someC is a C of any type) will try to assign a C<X> to an int
    2. operator T<int> will be called on someC which will return a C<int> and then try to assign it to an int
    3. operator T<int> will be called on the aforementioned return value which will return a C<int>
    4. That return value which is a C<int> will attempt to be converted to an int which will call operator T<int>….
    5. goto 3;

    Hopefully you can see why the infinite recursion and subsequent stack overflow occurs.

    You cannot return a C<U> from operator T<U> of class C. You need to redesign your class if you need to for some reason.

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

Sidebar

Related Questions

I want to create a template class in C#, for example: public class Foo<T>
I want to align my member variables based on a class template type but
I have the following setup: A templated class SpecialModel: template<typename A, typename B> class
I have a class similar to the following: class SomeClass { public: template<typename... Args>
I have a class with a templated constructor for implicit move conversion, however this
Say I have a templated class like template <typename T> struct Node { //
Say I have a templated class: template <typename T> class foo { void do_someting(T
I want to have a template class that looks something like what I have
I have a template class where I want to use objects of that class
I have a templated C++ class that exposes a number of methods, e.g template<int

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.