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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:20:33+00:00 2026-06-06T19:20:33+00:00

This code is not compilable. I can’t find why in standard. Can someone explain?

  • 0

This code is not compilable.
I can’t find why in standard. Can someone explain?

#include <iostream>
#include <string>

template<typename T>
class S
{
public:
   explicit S(const std::string& s_):s(s_)
   {
   }
   std::ostream& print(std::ostream& os) const
   {
      os << s << std::endl;
      return os;
   }
private:
   std::string s;
};

template<typename T>
std::ostream& operator << (std::ostream& os, const S<T>& obj)
{
   return obj.print(os);
}

/*template<>
std::ostream& operator << <std::string> (std::ostream& os, const S<std::string>& obj)
{
   return obj.print(os);
}*/

class Test
{
public:
   explicit Test(const std::string& s_):s(s_)
   {
   }
   //operator std::string() const { return s; }
   operator S<std::string>() const { return S<std::string>(s); }
private:
   std::string s;
};

int main()
{
   Test t("Hello");
   std::cout << t << std::endl;
}

Compiler output:

source.cpp: In function 'int main()':
source.cpp:47:17: error: no match for 'operator<<' in 'std::cout << t'
source.cpp:47:17: note: candidates are:
In file included from include/c++/4.7.1/iostream:40:0,
                 from source.cpp:1:
include/c++/4.7.1/ostream:106:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
include/c++/4.7.1/ostream:106:7: note:   no known conversion for argument 1 from 'Test' to 'std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&) {aka std::basic_ostream<char>& (*)(std::basic_ostream<char>&)}'
....
  • 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-06T19:20:35+00:00Added an answer on June 6, 2026 at 7:20 pm

    Thats because no conversions, except for array-to-pointer, function-to-pointer, lvalue-to-rvalue and top-level const/volatile removal (cf. c++11 or c++03, 14.8.2.1), are considered when matching a template function. Specifically, your user-defined conversion operator Test -> S<string> is not considered when deducing T for your operator<< overload, and that fails.

    To make this universal overload work, you must do all the work at the receiving side:

    template <class T>
    typename enable_if<is_S<T>::value, ostream&>::type operator <<(ostream&, const T&);
    

    That overload would take any T, if it weren’t for the enable_if (it would be unfortunate, since we don’t want it to interfere with other operator<< overloads). is_S would be a traits type that would tell you that T is in fact S<...>.

    Plus, there’s no way the compiler can guess (or at least it doesn’t try) that you intended to convert Test to a S<string> and not S<void> or whatever (this conversion could be enabled by eg. a converting constructor in S). So you have to specify that

    • Test is (convertible to) an S too
    • the template parameter of S, when converting a Test, is string

    template <class T>
    struct is_S {
      static const bool value = false;
    };
    
    template <class T>
    struct is_S<S<T>> {
      static const bool value = true;
      typedef T T_type;
    };
    
    template <>
    struct is_S<Test> {
      static const bool value = true;
      typedef string T_type;
    };
    

    You will have to convert the T to the correct S manually in the operator<< overload (eg. S<typename is_S<T>::T_type> s = t, or, if you want to avoid unnecessary copying, const S<typename is_S<T>::T_type> &s = t).

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

Sidebar

Related Questions

Why this code does not compile (Cygwin)? #include <vector> template <class Ttile> class Tilemap
this code will not pass the query string to default3.aspx on Image Button ..
runat=server /> But this code is not working How can I add multiple controls
The error shows this code can not rollback: class AddCountToTag < ActiveRecord::Migration def change
Why does this code not print an exception stack trace? public class Playground {
This is probably a quicky. Why does this code not return anything? import java.util.Scanner;
Possible Duplicate: Comparing Strings in Cocoa Why is this code not recognising the NSString
This code does not throw an error but the query fails, that is, the
This code does not function as expected: // $field contains the name of a
I am unsure why this code will not work. When I click a button

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.