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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T04:07:42+00:00 2026-06-19T04:07:42+00:00

I want to implement a template function using nested-types of a template-class. I have

  • 0

I want to implement a template function using nested-types of a template-class.

I have just read here that it is better to implement operator << as non-member and non-friend function. Therefore I decided to move functions toStream() and tableToStream() outside MyClass:

template <typename T>
class MyClass
{
public:
  typedef boost::dynamic_bitset<> BoolTable; 
  typedef std::vector<T>          MsgTable;
private:
  BoolTable  b_;
  MsgTable   m_;
public:
  const BoolTable& getB() const { return b_; }
  const MsgTable & getM() const { return m_; }

  std::ostream& toStream (std::ostream& os) const
  {
    os <<"Bool: ";  tableToStream (os, getB());  os <<'\n';
    os <<"Msg:";    tableToStream (os, getM());  os <<'\n';
    return os;
  }

  template <typename TABLE>
  std::ostream& tableToStream (std::ostream& os, const TABLE& table) const
  {
    for (int i=0; i < table.size(); ++i)
      os << table[i] <<',';
    return os;
  }
};

template <typename T>
std::ostream& operator << (std::ostream& os, const MyClass<T> mc)
{
  return mc.toStream(os);
}

It’s easy to convert MyClass::toStream() into an operator << non-member and non-friend function:

template <typename T>
std::ostream& operator << (std::ostream& os, const MyClass<T>& mc)
{
  os <<"Bool: ";  mc.tableToStream (os, mc.getB());  os <<'\n';
  os <<"Msg:";    mc.tableToStream (os, mc.getM());  os <<'\n';
   return os;
}

But I want to use solely operator << instead of calling MyClass::tableToStream():

template <typename T>
std::ostream& operator << (std::ostream& os, const MyClass<T>& mc)
{
  os <<"Bool: "<< mc.getB() <<'\n';
  os <<"Msg:"  << mc.getM() <<'\n';
   return os;
}

For the function MyClass::tableToStream() I could use the following implementation, but this may mess the stream output because the function is too generic (any type can be TABLE).

template <typename TABLE>
std::ostream& operator << (std::ostream& os, const TABLE& table) 
{
  for (int i=0; i < table.size(); ++i)
    os << table[i] <<',';
  return os;
}

Therefore, I want to restrict to the nested types of MyClass. Below is one of my attempts to convert MyClass::tableToStream() into a standard operator << non-member and non-friend function:

template <typename T, typename MyClass<T>::TABLE>
std::ostream& operator << (std::ostream& os, const TABLE& table) 
{
  for (int i=0; i < table.size(); ++i)
    os << table[i] <<',';
  return os;
}

But the error is about typename MyClass<T>::TABLE.

  • 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-19T04:07:43+00:00Added an answer on June 19, 2026 at 4:07 am

    Since you have clarified your question a lot, my first answer does not apply any more and I’ll remove-edit it to give you something that might fit better:

    Updated answer:
    You want to constrain the template to accept only types that are typedeffed inside your MyClass template. Such constraints are usually achieved by application of SFINAE, especially by std::enable_if (or boost::enable_if, if your library lacks that part of C++11 support). Sadly, there is no traits like a is_typedeffed_inside that could be used for your case. It’s even worse: there is no way to write such a trait just using the plain typedefs, since there is nothing special about being typedeffed inside a given class – the compiler has no way to determine (and is not interested in) if a given known type has some alias name for it somewhere.

    But if your typedefs are just the ones you show in your question, I have good news for you: you need exactly two operator<< for that:

    1. One for boost::dynamic_bitset<>, since that is the BoolTable for any MyClass instantiation.
    2. Another one, templated, for std::vector<T>, since that is the MsgTable for each corresponding MyClass<T>.

    The downside is, that with this templated operator<<, you’d be able to output any std::vector<FooBar>, even if FooBar is completely unrelated to any use of MyClass. But that holds for any other possible implementation of the proper operator<<‘s – if there’s no explicit restriction on the MSG parameter, there’s no restriction on a FooBar making a std::vector<FooBar> a viable MyClass<MSG>::MsgTable.

    My conclusion for your question: you wanted to have the operator<< for its convenient looks, since it is normally used for thet purpose. In your case, you can provide it for MyClass<MSG> objects, but there is no way to do so for the inner typedefs alone.

    I’d implement it that way:

    template <class MSG>
    class MyClass {
      /* ... */
    public:
      // instead of declaring op<< a friend, redirect to a method that has 
      // natural access to private members
      std::ostream& printToStream(std::ostream& os) const
      {
        os << "Bool: "; 
        tableToStream (getB(), os); 
        os <<"\nMsg:";   
        tableToStream (getM(), os); 
        return os <<'\n';
      }
    private:
      // make this one private so nobody can misuse it to print unrelated stuff
      template <class Table>
      static void tableToStream(Table const& table, std::ostream& os)
      {
        std::copy(begin(table), end(table), ostream_iterator(os, ", "));    
      }
    };
    
    template <typename MSG>
    std::ostream& operator << (std::ostream& os, const MyClass<MSG>& mc)
    {
      return mc.printToStream(os); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to specialize a class template with the following function: template <typename T>
I have a bash function that fetches values (using curl and cut) and creates
In my project, I want to implement a template proxy class of some existing
I'm using Visual Studio 2008, and I want to implement string formatting function without
i starter in jqgrid, i want implement inline edit in jqgrid i have this
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
Microsoft has announce that WindowsLiveID become a OpenID provider . I want implement it
I want to implement login using facebook in my windows phone 7.1 application When
Here's a jsFiddle that shows what I'm trying to do: http://jsfiddle.net/P3c7c I'm using the
I have several templated objects that all implement the same interface: I.E. MyObject<datatype1> obj1;

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.