0
#include <iostream>

using namespace std;

int main()
{
    auto a{1};
    auto b{1};
    if (a==b)
    {
        cout << "equal";
    }
    return 0;
}

Why does the above C++ code return an error in g++ compiler with c++11 standard, instead of printing "equal" as output?

test.cpp:9:14: error: no match for ‘operator==’ (operand types are ‘std::initializer_list’ and ‘std::initializer_list’) if (a==b) ^

erip
  • 16,374
  • 11
  • 66
  • 121
Jayanth Reddy
  • 347
  • 1
  • 3
  • 7

1 Answers1

4

What do you think:

auto a{1};

is to compiler?

If you think it's supposed to be integer you are wrong. Compilers are lazy in determining what things are, and it's initializer_list (just as your error stated) And std::initializer_list doesn't have == operator defined (just as your compiler stated)

erip
  • 16,374
  • 11
  • 66
  • 121
MaciekGrynda
  • 583
  • 2
  • 13
  • the problem was with the compiler which was outdated, i updated it and it works perfectly – Jayanth Reddy Jun 29 '16 at 13:25
  • I don't think it's compiler issue, it's most likely that newer ones provide a shortcut. For example I use VS 2015 and it gives me the same error as you has, so there will most likely problems with code maintenance – MaciekGrynda Jun 29 '16 at 13:37
  • This is not correct. From cppreference: _If the braced-init-list has only one element and either T isn't a reference type or is reference type that isn't compatible with the type of the element, T is direct-initialized (in direct-list-initialization) or copy-initialized (in copy-list-initialization), except that narrowing conversions are not allowed._ – erip Jun 29 '16 at 14:04
  • [clang](http://coliru.stacked-crooked.com/a/14588dc3469c5740), [g++](http://coliru.stacked-crooked.com/a/b442311eb0a64587). – erip Jun 29 '16 at 14:06
  • 1
    @erip - you're right when `T` is a known type; something as `int a{1}`; but when you write `auto a{1}` you have to determine `T`, so (if I'm not wrong; take my words with a pinch of salt) you rule don't apply and `a` become an `std::initializer_list` – max66 Jun 29 '16 at 14:44
  • @erip - http://en.cppreference.com/w/cpp/language/auto `auto c = {1, 2}`and `typeid(c).name()` gives `std::initializer_list` – MaciekGrynda Jun 29 '16 at 15:13
  • @erip - Interesting; you're code doesn't compile in my laptop (g++ 4.9.2 and clang++ 3.5) so I've tried it in Wandbox (Melpon.org); adding a `""` in `static_assert` (you use as in C++14), your example compile and run starting from g++ 5.1 and from clang++ 3.8. So, I suppose you're right. – max66 Jun 29 '16 at 15:30
  • @max66 but here: http://stackoverflow.com/questions/23406212/why-does-auto-a-1-compile-in-c is kind of different explanation. I'll start new question maybe. – MaciekGrynda Jun 29 '16 at 15:40
  • @erip - seems that `auto a{1}` is a C++17 feature that `g++` and `clang++` adopt for C++11 and C++14 too; see the [new question of MaciekGrynda](http://stackoverflow.com/questions/38104782/using-auto-with-initializer-list) and the first answer to [this question](http://stackoverflow.com/questions/25612262/why-does-auto-x3-deduce-an-initializer-list) – max66 Jun 29 '16 at 18:25
  • It's a big can of worms here. :) – erip Jun 29 '16 at 19:17
  • @erip Actually in this case, VS's compiler was actually standard too fast in order to minimize the anticipated breaking change – KABoissonneault Jun 29 '16 at 19:28