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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:53:34+00:00 2026-05-27T20:53:34+00:00

I would like to overload the operator<< to allow it to work with shared_ptr

  • 0

I would like to overload the operator<< to allow it to work with shared_ptr.

template<typename T>
struct foo
{
    virtual foo& operator<<(const T& e) = 0;
};

foo<int> f1;
f1 << 1;

std::shared_ptr<foo<int>> f2(new foo<int>());
f2 << 1;

My first try is the following, but the problem is that it with also enable the behavior for any class.

template<typename T, typename U>
const std::shared_ptr<T>& operator<<(const std::shared_ptr<T>& o, const U& e)
{
    *o << e;
    return o;
}

My second try is the following:

template<typename T, typename U>
const std::shared_ptr<foo<T>>& operator<<(const std::shared_ptr<foo<T>>& o, const U& e)
{
    *o << e;
    return o;
}

The problem with this solution is not work for types inheriting foo since T cannot be automatically deduced.

So I could skip U and use T instead, in which case T will be deduced from the second argument and the argument for o can be converted into foo<T>.

template<typename T, typename U>
const std::shared_ptr<foo<T>>& operator<<(const std::shared_ptr<foo<T>>& o, const T& e)
{
    *o << e;
    return o;
}

But then the following will not work:

struct c    
{    
};

struct a
{
    a();
    a(c); // implicit conversion
};

struct b
{
    operator a(); // implicit conversion
};

auto f = std::make_shared<foo<a>>();
f << c; // doesn't work.
f << b; // doesn't work.

Any ideas on how make a working solution?

  • 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-27T20:53:34+00:00Added an answer on May 27, 2026 at 8:53 pm

    Some options, see the second live at https://ideone.com/26nqr

    Given

    #include <iostream>
    #include <memory>
    using namespace std;
    
    template<typename T> struct foo {
        virtual foo& operator<<(const T& e) const { std::cout << "check foo\n"; }
    };
    
    //////
    // derived instances
    
    struct derived : foo<int> {
        virtual derived& operator<<(const int& e) const { std::cout << "check derived\n"; }
    };
    
    template<typename T> struct genericDerived : foo<T> {
        virtual derived& operator<<(const T& e) const { std::cout << "check genericDerived\n"; }
    };
    

    Simple: template template arguments

    template<typename T, typename U, template <typename> class X>
    const std::shared_ptr<X<T>>& operator<<(const std::shared_ptr<X<T>>& o, const U& e)
    {
        *o << e;
        return o;
    }
    
    int main()
    {
        auto f = make_shared<foo<int>>();
        f << 1;
    
        auto d = make_shared<derived>();
        d << 2; // compile error
    
        auto g = make_shared<genericDerived<int>>();
        g << 3; // SUCCESS!
    }
    

    Completer: SFINAE

    The above doesn’t catch derived classes (case 2). To do that, I’d resort to

    #include <type_traits>
    namespace detail
    {
        template<typename Foo, typename T>
            const std::shared_ptr<Foo>& dispatch_lshift(
                      const std::shared_ptr<Foo>& o, const T& e, 
                      const std::true_type& enabler)
            {
                *o << e;
                return o;
            }
    }
    
    template<typename Foo, typename T>
        const std::shared_ptr<Foo>& operator<<(const std::shared_ptr<Foo>& o, const T& e)
    {
        return detail::dispatch_lshift(o, e, std::is_convertible<Foo*, foo<T>* >());
    }
    
    int main()
    {
        auto f = make_shared<foo<int>>();
        f << 1;
    
        auto d = make_shared<derived>();
        d << 2;
    
        auto g = make_shared<genericDerived<int>>();
        g << 3;
    
        auto x = make_shared<int>();
        // x << 4; // correctly FAILS to compile    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The min algorithm is normally expressed like this: template <typename T> const T& min(const
I would like to overload the (/) operator in F# for strings and preserve
Is it possible to overload [] operator twice? To allow, something like this: function[3][3]
I would like to overload the + operator of iterator in list class, something
I'm trying to overload the assignment operator and would like to clear a few
I would like to overload << operator for chaining like below function1(param1, param2)<< obj2
I'm using Excel VBA to a write a UDF. I would like to overload
Instead of having an add() method in the repository, I would like to overload
Consider the following class: class MyClass { public: template<class T> typename T::result_type apply(T& func)
Having MyClass : QPolygonF I would like to create void bin_write(QDataStream & out )

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.