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

  • Home
  • SEARCH
  • 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 7698739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:18:45+00:00 2026-05-31T22:18:45+00:00

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

  • 0

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?

  • 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-05-31T22:18:46+00:00Added an answer on May 31, 2026 at 10:18 pm
    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.

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

Sidebar

Related Questions

Compiling this code snippet with gcc (4.5) and as many -Wall, -Wextra, -Wuninitialized type
I am compiling this code with g++: #include <pthread.h> #include <iostream> #include <cstdlib> #include
Take this non-compiling code for instance: public string GetPath(string basefolder, string[] extraFolders) { string
I am new in C and I have problem with compiling this code. #include
I have the following code #include <iostream> #include <vector> using namespace std; int distance(vector<int>&
Can someone help me out here? Compiling this code: void test() { std::set<int> test;
When compiling this code I get: fatal error C1083: Cannot open include file: 'fstream.h':
I get an undefined reference to 'typeof'-error compiling and linking this: #include <stdio.h> #include
Compiling this code int main(int argc, char **argv) { int xor = 0; }
I was trying to understand something with pointers, so I wrote this code: #include

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.