1

So this code is for a command input to be entered in any random order and it will return the value that comes after your input. Amt_Range is a digit checking function.

Why does this work. It should be able to due to the pointer comparison.??

More importantly what does the string() do. By my limited understanding the comparison should not work because a c style string is in the form '-' 't'. Thanks in advance!!

int main(int argc, const char *argv[]) {

string dummy;
int tests = 0, quizzes = 0, assignments = 0, labs = 0, fin = 0;
int testweight = 0, quizweight = 0, assignweight = 0, labweight = 0, finweight = 0;
int counter = 1;

    if (argv[counter] == string("-t")) {
        dummy = argv[counter + 1];
        tests = Amt_Range(dummy, "tests");
        counter+=2;

    } else if (argv[counter] == string("-q")) {
        dummy = argv[counter + 1];
        quizzes = Amt_Range(dummy, "quizzes");
        counter+=2;


    } else if (argv[counter] == string("-a")) {
        dummy = argv[counter + 1];
        assignments = Amt_Range(dummy, "assignments");
        counter+=2;


    } else if (argv[counter] == string("-l")) {
        dummy = argv[counter + 1];
        labs = Amt_Range(dummy, "labs");
        counter+=2;


    } else if (argv[counter] == string("-f")) {
        dummy = argv[counter + 1];
        fin = Amt_Range(dummy, "whether there is a final");
        counter+=2;

    } else {
    cout << "wrong input NOW START OVER" << endl;
    exit(EXIT_FAILURE);

    }
}
vsoftco
  • 55,410
  • 12
  • 139
  • 252
DisplayName
  • 196
  • 3
  • 12
  • Luckily it's not comparing pointers (because it would never compare equal). Also, c strings are not "in the form '-' 't'". C strings are either `char const*` or `char(&)[N]`. – sehe Nov 19 '14 at 23:18

2 Answers2

7

The operator==() overload that kicks in is a free function in the std namespace.

namespace std { 
     bool operator==(std::string const&, std::string const&);
}

It takes the first argument by const& which means that a temporary is welcome.

A temporary happens to be creatable using the implicit conversion constructor std::string(char const*). So, the overload applies.

UPDATE As uncovered in the comments, the standard actually declares

      bool operator==(const char*, std::string const&);

as an optimization in §21.4.8.2 operator==. However, the implicit conversion on the LHS is good to know about because it's a key ingredient in the language design with respect to operator overload (resolution)


Now, the reason why the overload bool std::operator==(std::string const&, std::string const&) is even found during overload resolution is slightly subtle. This mechanism is known as Argument Dependent Lookup.

In this case, ADL works because the second argument is already a std::string, which has a type declared in the std namespace. Therefore, this namespace is searched for candidate operator== overloads


DISCLAIMER:

I've simplified the above. In reality the code in the standard library is much more generic still, and probably looks more like

namespace std { 
     template <typename Char, typename CharTraits, typename Alloc>
     bool operator==(std::basic_string<Char, CharTraits, Alloc> const&, std::basic_string<Char, CharTraits, Alloc>  const&) {
          // implementation...
     }
}
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Did you mean `std::operator==(char const *, std::string const&)`? – vsoftco Nov 19 '14 at 23:18
  • @Ohh I see, never mind, it implicitly converts the `char const*` to `std::string` via the `std::string`'s ctor... – vsoftco Nov 19 '14 at 23:19
  • @vsoftco You got it! (This, by the way might not be the most efficient thing to do, so it's good to be aware of it. Modern C++ libraries tend to prefer `explicit` conversion constructors for this reason - and some other pitfalls unrelated) – sehe Nov 19 '14 at 23:22
  • @Deduplicator, agree, first I though that the overloads exist, but found now they don't. – vsoftco Nov 19 '14 at 23:22
  • Mmm. Let me check they don't actually exist :) I might just not remember – sehe Nov 19 '14 at 23:23
  • @sehe, it seems they exist actually, http://en.cppreference.com/w/cpp/string/basic_string/operator_cmp Would have been a bit weird not to, due to performance issues, as Deduplicator mentioned. – vsoftco Nov 19 '14 at 23:24
  • Okay. I've updated my answer to be more accurate in this particular case, though I kept my broader explanation involving implicit conversions on the LHS operand, because I think it will aid understanding to those learning C++. – sehe Nov 19 '14 at 23:32
  • 1
    Gotta say that I am a bit confused about the ADL. Why ADL here? The OP seems to have an `using namespace std;` already, so he's not calling `operator==(const char*, std::string)`. Sorry for the long line of comments, would like to understand what you meant by ADL kicking in here. – vsoftco Nov 19 '14 at 23:34
  • @vsoftco I have a blind spot for `using namespace std`. It shouldn't be used, so I assume it isn't used. In real life, production quality code, ADL would kick in here. (Just as in `std::cout << "hello world\n";` for that matter) – sehe Nov 19 '14 at 23:40
  • 1
    OK :) +1, great answer anyway! – vsoftco Nov 19 '14 at 23:40
  • I'm not sure it's wise to name any standard library function "an optimization". – Lightness Races in Orbit Nov 20 '14 at 00:58
  • @LightnessRacesinOrbit agreed. I pondered it. What would you call it, though? – sehe Nov 20 '14 at 01:25
  • 1
    ---Probably something along the lines of "for utility" or "for convenience". I dunno, something like that.--- Actually I guess they don't really fit; I've only just realised _why_ you were circling the "optimisation" route. Not sure I can come up with anything better. – Lightness Races in Orbit Nov 20 '14 at 01:26
2

Quote from the standard (C++14):

21.3 String classes [string.classes]

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

[... same for != < > <= >=]

Or look in this section:

21.4.8.2 operator== [string::operator==]

So there is an exact match, which is found in namespace std (would use ADL (argument-dependent lookup, counting the arguments namespaces as associated namespaces and searching them too) if there was no using-directive in scope).

BTW: I'm a bit disappointed that two std::basic_strings differing only in allocator-type cannot be compared (template-arguments must match exactly)...
A quick demo on coliru: http://coliru.stacked-crooked.com/a/01788a718178c6d2

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • Differently allocated strings _can_ be compared, as long as they have implicit conversions. I _know_ I've compared heterogenous strings in shared memory and otherwise before. Let me see how that actually worked (if I can still find it) – sehe Nov 19 '14 at 23:38
  • Must template-arguments not match exactly? – Deduplicator Nov 19 '14 at 23:39
  • I will look up the mechanics. But, you know what you know, and I already said what I think is the catch here (implicit conversion?). Hang on for a moment please – sehe Nov 19 '14 at 23:41
  • @sehe: See here for demonstration: http://coliru.stacked-crooked.com/a/01788a718178c6d2 – Deduplicator Nov 19 '14 at 23:44
  • Patience... [you don't have it](http://coliru.stacked-crooked.com/a/79ce7cc2dfd22d13) (fixed) :) Anyhoops, seems I misremembered, and it was for `operator<` anyways (see [here](http://stackoverflow.com/a/26454275/85371)). That's the same mixed `char*` vs. `basic_string<...>` overloading. Note however, not deducing a different set of template arguments enables implicit conversions to the `basic_string` type of the argument that matches, so that might be the **key** reason to not deduce different sets – sehe Nov 20 '14 at 00:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/65246/discussion-between-sehe-and-deduplicator). – sehe Nov 20 '14 at 00:20