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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:20:55+00:00 2026-06-02T14:20:55+00:00

Program 1: #include <iostream> #include <cstdlib> #include <vector> int main(){ //compiles successfully std::vector<int> vec{1,2,3,4,5};

  • 0

Program 1:

#include <iostream>
#include <cstdlib>
#include <vector>

int main(){

    //compiles successfully 
    std::vector<int> vec{1,2,3,4,5};

    return EXIT_SUCCESS;
}

Program 2:

#include <iostream>
#include <cstdlib>
#include <queue>

int main(){

    //compiler error
    std::queue<int> que{1,2,3,4,5};

    return EXIT_SUCCESS;
}

Error message:

main.cpp: In function ‘int main()’:
main.cpp:7:31: error: no matching function for call to ‘std::queue<int>::queue(<brace-enclosed initializer list>)’
main.cpp:7:31: note: candidates are:
/usr/include/c++/4.6/bits/stl_queue.h:141:7: note: std::queue<_Tp, _Sequence>::queue(_Sequence&&) [with _Tp = int, _Sequence = std::deque<int, std::allocator<int> >]
/usr/include/c++/4.6/bits/stl_queue.h:141:7: note:   candidate expects 1 argument, 5 provided
/usr/include/c++/4.6/bits/stl_queue.h:137:7: note: std::queue<_Tp, _Sequence>::queue(const _Sequence&) [with _Tp = int, _Sequence = std::deque<int, std::allocator<int> >]
/usr/include/c++/4.6/bits/stl_queue.h:137:7: note:   candidate expects 1 argument, 5 provided
/usr/include/c++/4.6/bits/stl_queue.h:92:11: note: std::queue<int>::queue(const std::queue<int>&)
/usr/include/c++/4.6/bits/stl_queue.h:92:11: note:   candidate expects 1 argument, 5 provided
/usr/include/c++/4.6/bits/stl_queue.h:92:11: note: std::queue<int>::queue(std::queue<int>&&)
/usr/include/c++/4.6/bits/stl_queue.h:92:11: note:   candidate expects 1 argument, 5 provided

Question:
why can’t queues be initialized like vectors?
I suppose they aren’t sequence containers, but why would that matter?
I’m sure there is a good reason, but I can’t find any explanations.

gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1

  • 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-02T14:20:56+00:00Added an answer on June 2, 2026 at 2:20 pm

    I don’t think it really has anything to do with being container adapters rather than containers (though I’ll admit I’m uncertain exactly why the correct constructor is omitted).

    When you use a braced initializer list with std::vector, you’re using this (new in C++11) constructor:

    vector(initializer_list<T>, const Allocator& = Allocator());
    

    Looking at the definition of std::queue, the available constructors are:

    explicit queue(const Container&);
    explicit queue(Container&& = Container());
    template <class Alloc> explicit queue(const Alloc&);
    template <class Alloc> queue(const Container&, const Alloc&);
    template <class Alloc> queue(Container&&, const Alloc&);
    template <class Alloc> queue(const queue&, const Alloc&);
    template <class Alloc> queue(queue&&, const Alloc&);
    

    A constructor taking an initialization_list is conspicuously absent.

    I’m quite certain that despite being a container adapter, such a constructor would be trivial if it was desired. Just for example:

    #include <deque>
    #include <initializer_list>
    #include <iostream>
    
    template <class T, class container=std::deque<T> >
    class myqueue {
        container data;
    public:
        explicit myqueue(std::initializer_list<T> t) : data(t) {}
        void pop() { data.pop_front(); }
        T front() const { return data.front(); }
        bool empty() const { return data.empty(); }
    };
    
    int main(){
        myqueue<int> data {1, 2, 3, 4};
        while (!data.empty()) {
            std::cout << data.front() << "\n";
            data.pop();
        }
        return 0;
    }
    

    g++ 4.7 accepts this without any problems, and produces exactly the output you’d expect:

    1
    2
    3
    4
    

    Although I haven’t tested with any other compilers, I can’t see any reason other compilers wouldn’t work fine with this as well (provided they implement the necessary features, of course).

    Edit: I just did some looking through the committee papers proposing the addition of initalizer_lists to C++ (e.g., N1890, N1919, N2100, N2215, N2220) and it looks to me like a simple oversight. Many of the earlier papers are more conceptual, but N2220 has a fair amount of proposed language for the working paper. For std::array (for one example) it specifically points out that no change is needed. It then goes through deque, vector, [unordered_][multi_](set|map), and shows changes needed for each — but no mention is made of stack or queue at all, in either direction. No proposal to add support for std::initializer_list, nor (like std::array) reasoning for their omission.

    I’d conclude that it was a simple oversight, that probably slipped through for two reasons: 1) the adapters are almost, but not quite containers, and 2) the adapter classes don’t seem to be used a whole lot, so forgetting about them was probably fairly easy (and, of course, the ever-pervasive third reason: most of the active committee members are horribly overworked).

    Edit2: I should probably add one more detail: since stack and queue can both accept another container for the initialization, you can pretty easily do something like:

    std::stack<int> data(std::vector<int>{1,2,3,4});
    

    This is somewhat verbose, but unlikely to cause any loss of efficiency (the container will be passed as an rvalue reference, so its representation will be “stolen” instead of copied). There is one caveat though: if the type of container you use doesn’t match the container underlying the container adapter, you’ll get a copy rather than a move (and consequently, may lose some efficiency).

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

Sidebar

Related Questions

Take the following program: #include <cstdlib> using std::rand; #include <iostream> using std::cout; int main()
This is a small program: #include <iostream> #include <cstdlib> using namespace std; int main()
I created this program: #include <iostream> #include <fstream> using namespace std; int main ()
The C++ program #include <complex> #include <iostream> int main() { std::complex<double> z(0,2); int n
This program: #include <iostream> #include <cstdlib> #include <string> int main(int argc, const char *argv[])
#include<iostream> #include<fstream> #include<cstdlib> #include<string> using namespace std; **int main() { double write(); double read();
I have such a program: #include <stdlib.h> #include <iostream> static int pswd=0; int main()
I'm trying to compile the following program: #include<functional> #include<iostream> int main(int argc, char* argv[],
I have following code #include <iostream> #include<exception> #include <cstdlib> int main(){ for (int i
Consider the following program #include <iostream> #include<cstdlib> using namespace std; class E { public:

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.