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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:57:09+00:00 2026-06-13T14:57:09+00:00

I need to check if a given class has the <<(cls, ostream) operator defined

  • 0

I need to check if a given class has the <<(cls, ostream) operator defined or not. If so, I want my function to use that to write to ostringstream, otherwise boilerplate code should be used.

I know this question has been asked before. However, I generally find custom solutions that don’t always work on my compiler (clang++). After many hours of searching, I finally found out that boost::type_traits. I hadn’t look there previously, because I had assumed c++11 already copied everything in the traits department that boost had.

The solution that worked for me was to do:

template <typename C>
std::string toString(C &instance) {
    std::ostringstream out;
    out << to_string<C, boost::has_left_shift<C, std::ostream>::value>::convert(ctx);
    return out.str();
}

with to_string defined as:

template <typename C, bool>
struct to_string {
    // will never get called
    static std::string convert(LuaContext &ctx) {}
};

template <typename C>
struct to_string<C, true> {
    static std::string convert(LuaContext &ctx) {
        return "convert(true) called.";
    }
};

template <typename C>
struct to_string<C, false> {
    static std::string convert(LuaContext &ctx) {
        return "convert(false) called.";
    }
};

So I’m posting this for two reasons:

  1. Check if this is the sanest method to use, or see if someone else can suggest an even better solution (i.e. the question is more out of curiosity of approach than “will this work?” — it already works for me)

  2. Post this up to save someone else hours of searching in case she/he also needs to do something similar.

  3. As a more general question — sometimes trait classes appear to return std::true_type or std::false_type (well, at least for non-boost classes). Other times they are bools. Is there a reason for this discrepancy? If boost:has_left_shift returned a type instead of a bool, then I could have just a single to_string struct.

  • 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-13T14:57:10+00:00Added an answer on June 13, 2026 at 2:57 pm

    Quick-and-dirty C++11 SFINAE:

    template<typename T,
             typename = decltype(
               std::declval<std::ostream&>() << std::declval<T const&>()
             )
    >
    std::string toString(T const& t)
    {
        std::ostringstream out;
        // Beware of no error checking here
        out << t;
        return out.str();
    }
    
    template<typename T,
             typename... Ignored
    >
    std::string toString(T const& t, Ignored const&..., ...)
    {
        static_assert( sizeof...(Ignored) == 0
                     , "Incorrect usage: only one parameter allowed" );
        /* handle any which way here */
    }
    

    If you want you can also check that the return type of stream << val is indeed convertible to std::ostream&:

    template<typename T,
             typename Result = decltype(
               std::declval<std::ostream&>() << std::declval<T const&>()
             ),
             typename std::enable_if<
                 std::is_convertible<Result, std::ostream&>::value,
                 int
             >::type = 0
    >
    

    As for a not so quick-and-dirty solution I’d introduce an is_stream_insertable trait, which implementation can make use of the very same tricks used here.

    Be aware that std::integral_constant<bool, B> has a conversion operator to bool, this might explain some of the things you have observed. I also do not recommend mixing the C++11 Standard types and traits with Boost: don’t mix up std::true_type with boost::true_type! Which is not to say that you shouldn’t use e.g. Boost.TypeTraits at all with C++11, but try to be consistent and only use one of two at a time.

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

Sidebar

Related Questions

I need to check whether given binary file has write access or not. File
I need to check if my address has any words that matches word in
I'm using a third party javascript that has given me a lot of need
I need to check whether a given date is in the preceding month for
i need to check existing of Character (*,&,$) in Given String using python command
I need to check a variable vi_theIndex for its value. At the given moment
I need to check if a link (a-tag) is the only content (not just
I need just to clarify that given collection contains an element. I can do
Given a base class that is inherited by plethora of derived classes, and a
I am implementing a class that resembles a typical database table: has named columns

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.