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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:28:37+00:00 2026-05-28T13:28:37+00:00

I have the following code. Basically I want to initialize a std::array of non-POD

  • 0

I have the following code. Basically I want to initialize a std::array of non-POD structs using aggregate initialization syntax. Both g++ 4.6 and 4.7 (latest weekly snapshot) fails to compile the code.

#include <array>

struct TheClass {

    int a1, a2;

    TheClass(int b1, int b2) : a1{b1}, a2{b2} {};    
};

template<unsigned D>
struct OtherClass {

    std::array<TheClass, D> a;

    OtherClass(std::array<TheClass, 2> b) : a{b} {};
};

int main()
{
    OtherClass<2>{{ {1, 2}, {2, 3} }}; //tried a lot of options here
}

GCC errors:

v.cpp: In function ‘int main()’:
v.cpp:20:37: error: no matching function for call to ‘OtherClass<2u>::OtherClass(<brace-enclosed initializer list>)’
v.cpp:20:37: note: candidates are:
v.cpp:15:5: note: OtherClass<D>::OtherClass(std::array<TheClass, 2ul>) [with unsigned int D = 2u]
v.cpp:15:5: note:   no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘std::array<TheClass, 2ul>’
v.cpp:11:8: note: constexpr OtherClass<2u>::OtherClass(const OtherClass<2u>&)
v.cpp:11:8: note:   no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘const OtherClass<2u>&’
v.cpp:11:8: note: constexpr OtherClass<2u>::OtherClass(OtherClass<2u>&&)
v.cpp:11:8: note:   no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘OtherClass<2u>&&’

My questions: is the above code correct? It seems that as std::array is an aggregate, there should be no problems to construct its data members. Maybe it’s a bug in GCC?

EDIT:

OtherClass<2>{{ TheClass{1, 2}, TheClass{2, 3} }}; works of course, but I don’t want to use that as I have to construct the class in a lot of places. C++11 should support omission of TheClass. Also see this question.

  • 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-28T13:28:38+00:00Added an answer on May 28, 2026 at 1:28 pm

    std::array is an aggregate containing an array, so you need an extra pair of braces to initialise it from a regular array:

    {1,2}               // TheClass
    {{1,2},{2,3}}       // TheClass[2]
    {{{1,2},{2,3}}}     // std::array<TheClass,2>
    {{{{1,2},{2,3}}}}   // OtherClass<2>
    

    However, it seems that older versions of GCC still stuggle with this: 4.5.1 rejects it, while 4.6.1 accepts it.

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

Sidebar

Related Questions

I'm having an issue with rails involving javascript. Basically, I have the following code:
i have the following jquery code. basically i will have several overlapped divs and
I have the following code: jQuery('#hexVal').change(function() { var hexvarval = jQuery('#hexVal').val(); jQuery('#thumbnail_hover_color').val(hexvarval); }); Basically,
Hi I have the following code and I only want to pull out links
I have the following code while($row = $usafisRSP->fetch_assoc()) { $hidden_keys = array('Applicantid', 'unique_num', 'regs_time'
I have the following code in a controller: $this->viewdata->scripts = array( 'http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js', 'jstree/jquery.jstree-1.0-rc2.js', 'jquery.hotkeys-0.8.js',
Hello in my aspx page using MVC 3, I have the following code: <%Response.WriteFile(/Content/Bing.htm);
I have the following code: $routes = array( 'twitter_output' => new Zend_Controller_Router_Route( 'twitter/:username\.:format', array('controller'
I have the following code in C, Its basically a maze where S=starting point
So I have the following code which i basically just a JSON string I

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.