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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:05:20+00:00 2026-06-14T07:05:20+00:00

I have already looked at a couple questions on this, specifically Overloading operator<<: cannot

  • 0

I have already looked at a couple questions on this, specifically Overloading operator<<: cannot bind lvalue to ‘std::basic_ostream<char>&&’
was very helpful. It let me know that my problem is I’m doing something that c++11 can’t deduce the type from.

I think a big part of my problem is that the instantiated class I’m working with, is templated, but originally obtained from a pointer to a non-template base class. This is something I did advised from another stackoverflow.com question about how to put template class objects into an STL container.

My classes:

class DbValueBase {
  protected:
    virtual void *null() { return NULL; }   // Needed to make class polymorphic
};

template <typename T>
class DbValue : public DbValueBase {
  public:
    DbValue(const T&val)  { data = new T(val); }
    ~DbValue() { if (data) delete data; }

    T   *data;

    const T&    dataref() const { return *data; } 

    friend std::ostream& operator<<(std::ostream& out, const DbValue<T>& val)
    {
        out << val.dataref();
        return out;
    }
}

And, the code snippet where the compile error database.cc:530:90: error: cannot bind ‘std::basic_ostream<char>’ lvalue to ‘std::basic_ostream<char>&&’ occurs:

//nb:  typedef std::map<std::string,DbValueBase*>  DbValueMap;
    const CommPoint::DbValueMap&    db_values = cp.value_map();
    for (auto i = db_values.cbegin() ; i != db_values.cend() ; i++) {
        // TODO: Need to implement an ostream operator, and conversion
        // operators, for DbValueBase and DbValue<>
        // TODO: Figure out how to get a templated output operator to
        // work... 
 //     DbValue<std::string> *k = dynamic_cast<DbValue<std::string>*>(i->second);
        std::cerr << "  Database field " << i->first << " should have value " << *(i->second) << endl;
    }

If my output tries to print i->second, it compiles and runs, and I see the pointer. If I try to output *(i->second), I get the compile error. When single-stepping in gdb, it seems to still know that i->second is of the correct type

(gdb) p i->second
$2 = (DbValueBase *) 0x680900
(gdb) p *(i->second)
warning: RTTI symbol not found for class 'DbValue<std::string>'
$3 = warning: RTTI symbol not found for class 'DbValue<std::string>'
{_vptr.DbValueBase = 0x4377e0 <vtable for DbValue<std::string>+16>}
(gdb) quit

I’m hoping that I’m doing something subtly wrong. But, it’s more complicated than I seem to be able to figure it out on my own. Anyone else see what thing(s) I’ve done wrong or incompletely?

Edit:

@PiotrNycz did give a good solution for my proposed problem below. However, despite currently printing values while doing development, the real need for these DbValue<> objects is to have them return a value of the correct type which I can then feed to database operation methods. I should’ve mentioned that in my original question, that printing is of value, but not the end of my goal.

  • 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-14T07:05:21+00:00Added an answer on June 14, 2026 at 7:05 am

    Although the debugger correctly identifies *(i->second) as being of the type DbValue<std::string>, that determination is made using information that is only available at runtime.

    The compiler only knows that it is working with a DbValueBase& and has to generate its code on that basis. Therefore, it can’t use the operator<<(std::ostream&, const DbValue<T>&) as that does not accept a DbValueBase or subclass.


    For obtaining the contents of a DbValue<> object through a DbValueBase&, you might want to loop into the Visitor design pattern.

    Some example code:

    class Visitor {
    public:
        template <typename T>
        void useValue(const T& value);
    };
    
    class DbValueBase {
    public:
        virtual void visit(Visitor&) = 0;
    };
    
    template <class T>
    class DbValue : public DbValueBase {
    pblic:
        void visit(Visitor& v) {
            v.useValue(m_val);
        }
    private:
        T m_val;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have already looked at other related questions, and none of them helped this
i know, similar questions have been asked, and i've already looked through everything i
I have already looked at this question but there was no solution. Here is
Have already looked at this stack question , at which point I looked at
I have already looked at other questions etc and i've seen you can't have
I have already looked at this similar question but i am still wondering if
Quick question (and sorry if it's already been asked, I have looked but couldn't
I have already download and install this package to support HTML5 input type http://support.microsoft.com/kb/2468871
Ok I've looked everywhere for this. I'm cutting out a couple of the variable
I am trying to disable validators using jquery. I have already looked Disable ASP.NET

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.