6
votes

Compiling this code with g++ 4.7.0 (-Wall -Wextra -Werror -Wconversion -std=c++11):

#include <iostream>  // std::cout, std::endl
#include <string>    // std::string
#include <utility>   // std::move

void out(std::string const &message)
{
   static int count{0};
   std::cout << count++ << " = " << message << std::endl;
}

struct Foo
{
   Foo()                         {out("constructor");}
  ~Foo()                         {out("destructor");}
   Foo(Foo const &)              {out("copy constructor");}
   Foo & operator=(Foo const &)  {out("copy via assignment"); return *this;}
   Foo(Foo &&)                   {out("move constructor");}
   Foo & operator=(Foo &&)       {out("move via assignment"); return *this;}
};

int main()
{
   auto bar{std::move(Foo())};
   out("exiting main");
}

...results in the following error:

error: unused variable 'bar' [-Werror=unused-variable]

I can remove the error by changing the bar initialization to any of the following:

/* 0 */ auto bar(std::move(Foo()));
/* 1 */ Foo bar{std::move(Foo())};
/* 2 */ Foo bar(std::move(Foo()));
/* 3 */ auto bar = std::move(Foo());
/* 4 */ Foo bar = std::move(Foo());
/* 5 */ auto bar __attribute__((unused)) {std::move(Foo())};

Once the bar initialization has been changed, the output is always:

0 = constructor
1 = move constructor
2 = destructor
3 = exiting main
4 = destructor

Why does the original bar initialization report an unused variable?

2
What happens at runtime if you use the "bad" version of the code and compile without -Werror? - John Zwinck
@JohnZwinck the error turns into a warning - Matt Tardiff
Seems like it's just an unhandled case in the unused variable warning logic. File a bug with gcc. - bames53
I said "at runtime." I meant please run the program after compiling it without -Werror, and tell us what you see. Does it do the same as the other versions of the code, or does it print nothing, or what? - John Zwinck
@JohnZwinck My mistake. At runtime, it outputs the same as the other versions. - Matt Tardiff

2 Answers

8
votes
auto bar{std::move(Foo())};

After this declaration, bar is of type std::initializer_list<Foo>, which has trivial copy/move operations and destructor. Your other declarations

auto bar(std::move(Foo()));
Foo bar{std::move(Foo())};
Foo bar(std::move(Foo()));
auto bar = std::move(Foo());
Foo bar = std::move(Foo());

declare bar as Foo or Foo&&, which suppresses the warning since it has nontrivial special member functions.

You generally don't want braced initialization with auto unless you specifically intend to create a std::inializer_list object.

2
votes

Well, bar is unused. You may want to file a defect for the compiler as this seems to errornously go undetected in the other situations.