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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:40:35+00:00 2026-06-13T23:40:35+00:00

From Guru of the Week #2 . We have the original function: string FindAddr(

  • 0

From Guru of the Week #2. We have the original function:

string FindAddr( list<Employee> l, string name )
{
    for( list<Employee>::iterator i = l.begin(); // (1)
         i != l.end(); 
         i++ ) 
    {
        if( *i == name ) // (2)
        {
            return (*i).addr;
        }
    }
    return "";
}

I added dummy Employee class to that:

class Employee
{
    string n;
public:
    string addr;

    Employee(string name) : n(name) {}
    Employee() {}

    string name() const
    {
        return n;
    }

    operator string()
    {
        return n;
    }
};

And got compilation error:

In place (1):

conversion from ‘std::_List_const_iterator<Employee>’ to non-scalar type ‘std::_List_iterator<Employee>’ requested

In place (2):

no match for ‘operator==’ in ‘i.std::_List_iterator<_Tp>::operator* [with _Tp = Employee]() == name’

To eliminate first one, we change iterator to const_iterator. And the only way to eliminate second error, is to write own operator==. However, Herb Sutter wrote that:

The Employee class isn’t shown, but for this to work it must either have a conversion to string or a conversion ctor taking a string.

But Employee has a conversion function and conversion constructor. GCC version 4.4.3. Compiled normally, g++ file.cpp without any flags.

There should be implicit conversion and it should work, why it doesn’t? I don’t want operator==, I just want it work like Sutter said, with a conversion to string or a conversion ctor taking a string.

  • 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-13T23:40:36+00:00Added an answer on June 13, 2026 at 11:40 pm

    Herb Sutter is wrong in this case (I don’t have a copy of “Exceptional C++”, but I’d expect this GotW entry to be cleaned up for the book.)

    But first, in order to get to the error in question, you have to remove the const from the l parameter declaration. (Note, that replacing iterator with const_iterator will only obfuscate the problem: your operator string() is not const, meaning that it is not callable for constant object *i).

    Once you fix the first problem, your code will indeed fail to compile at

    if( *i == name )
    

    line. This happens because function std::operator == that compares std::string objects is actually defined by the Standard Library as a template function

    template<class charT, class traits, class Allocator>
    bool operator==(
      const basic_string<charT,traits,Allocator>& lhs,
      const basic_string<charT,traits,Allocator>& rhs);
    

    In order for this function to participate in overload resolution, its template arguments have to be successfully deduced. This is impossible in your context, since in *i == name one argument is std::string and another is Employee. Template argument deduction fails and, for this reason, this template function is not considered for overload resolution. Having no other candidates, the compiler reports an error.

    For this reason Herb Sutter’s claim that the code should be compilable in presence of operator string() conversion function in Employee class is incorrect. The code might compile with some specific implementation of Standard Library that declares a dedicated non-template comparison operator for std::string, but normally Standard Library implementations don’t do it that way.

    He also makes another unfounded claim, insisting that the result of that conversion must be a temporary. In reality Employee class can have operator const string &() const conversion function, which would create no temporaries (return a reference to a data member instead, as it could be done in your example).

    Finally, his claim that conversion constructor will make this code work is only true if the program declares a dedicated operator == for Employee vs. Employee comparisons. Without introducing such dedicated operator, the conversion constructor will have no effect on the validity of this code. I.e. in your example there was no point in declaring Employee(string name) constructor – it does not achieve anything.

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

Sidebar

Related Questions

value = [python:guru-age20, is_it_possible_time100:goodTime99, hmm_hope_no_onecanansswer] How to get a particular string from this list
Hello guru of programming! Today I have got the task from my manager to
Say that I have some SELECT statement: SELECT id, name FROM people ORDER BY
In this article from Guru of the week, it is said: It is illegal
I need help from a SQL guru here... I have data in the following
I'm interested to hear feedback from a guru out there. If you are a
From my website, I have a button which calls a method and then generates
Im sure this will be pretty simple for a t-sql guru. I have the
I need an SQL guru to help me speed up my query. I have
I'm far from being a mysql guru, so need to know if there's a

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.