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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:33:55+00:00 2026-06-16T16:33:55+00:00

Consider the intention behind the following illegal C++11 code: struct Base { template<typename U>

  • 0

Consider the intention behind the following illegal C++11 code:

struct Base
{
    template<typename U>
    virtual U convert() = 0;
};

template<typename T>
struct Derived : Base
{
    T t;

    template<typename U>
    virtual U convert() { return U(t); }
};

struct Any
{
    Base* b;

    template<typename U>
    operator U() { return b->convert<U>(); }
};

int main()
{
    Any a = ...;
    string s = a; // s = a->b->t if T is convertible to string
                  //    fails otherwise with compile error or runtime exception
                  //                            (either acceptable)
}

Is there a way to achieve the same or similiar effect with legal code?

(fyi the above way is illegal because templates may not be ‘virtual’)

Update:

struct Base
{
    void* p;
    type_info type;
};

template<typename T>
struct Derived : Base
{
    Derived()
    {
         p = &t; // immovable
         type = typeid(T);
    }

    T t;
};

struct Any
{
    Base* b;

    template<typename T = U, typename U>
    operator U()
    {
        if (b->type != typeid(T))
           throw exception();

        T* t = (T*) b->p;

        return U(*t);
    }
};
  • 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-16T16:33:56+00:00Added an answer on June 16, 2026 at 4:33 pm

    Is this what you want?

    struct Base
    {
      virtual void* convert(const std::type_info&) = 0;
    };
    
    template<typename T>
    struct Derived : Base
    {
      virtual void* convert(const std::type_info& ti)
      { return typeid(T) == ti ? &t : nullptr; }
    
      T t;
    };
    
    struct Any
    {
      Base* b;
    
      template<typename U>
        operator U()
        {
          if (auto p = b->convert(typeid(U)))
            return *static_cast<U*>(p);
          throw std::exception();
        }
    };
    

    As the other answer says, it’s hard to know exactly what you want as you’ve only shown invalid code, not explained what you’re trying to achieve.

    Edit oh I see now you want it to work for any convertible type, not just exact matches … then no, you can’t turn a type_info back into the type it represents, which would be needed for the derived type to test if the given type_info corresponded to a type that its stored type is convertible to. You need to know the correct type and specify it somehow, either explicitly or implicitly via deduction. If you then want to convert it to another type, you can do that separately:

    Any a{ new Derived<int>() };
    try {
      char c = a;  // throws
    }
    catch (...)
    {
    }
    int i = a;        // OK
    char c = (int)a;  // OK
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {
consider the following code if insert() return the list itself. def sieve(l): if not
Consider this bit of obfuscated code. The intention is to create a new object
Consider the following code: public interface A { public A another(); } public interface
Consider the following snippet of code: foreach (var setting in RequiredSettings) { try {
Consider the following code. public interface IFoo { } public class Bar { public
Consider the following code : void ListenerImpl::attach(boost::shared_ptr<ISubscriber> subscriber) { boost::unique_lock<boost::mutex>(mtx); subscribers.push_back(subscriber); } void ListenerImpl::notify(MsgPtr
Consider the following code: class A { private: class B {}; public: B f();
Consider the following piece of mathematica code: a := {1, 2, 3}; f[n_, a_]
Consider the two following similar code samples. One where clause. bool validFactory = fields

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.