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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:21:44+00:00 2026-06-06T08:21:44+00:00

I am a little confused about what happens when I copy functors and/or initializers.

  • 0

I am a little confused about what happens when I copy functors and/or initializers. In the following code I thought I would copy/move the objects all the time but regardsless it Segfaults. I seem to be doing something wrong but haven’t figured out yet, what my wrong asumptions are.
Strangely at cppreference.com I could not find copy or move constructor for initializer_list so I wonder what actually happens in these cases.

#include <string>
#include <vector>
#include <functional>
#include <iostream>

std::initializer_list<std::function<std::string()>> getInitializer() {
  return {
    []() -> std::string {
      return "If";
    }
  };
}

int main() {
    std::function<int(std::string)> func;
    {
        auto init = getInitializer();

        func = [init](std::string text) -> int {
            std::vector<std::function<std::string()>> vec(init);

            for( auto& el : vec ) {
                std::cout << el();
            }
            std::cout << text << std::endl;
            return 5;
        };
    }

    return func(" you see this - the world is all right!");
}
  • 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-06T08:21:46+00:00Added an answer on June 6, 2026 at 8:21 am

    I don’t have much experience with initializer_lists, but the standard seems to suggest the implementation of an initializer_list is as if its a pair of pointers to an array. The list at getInitializer has automatic lifetime, and so does the array that backs it. You end up returning a pair of pointers to an array that no longer exists.

    The relevant sections of the standard are 8.5.4 [decl.init.list] items 5 and 6:

    5.- An object of type std::initializer_list<E> is constructed from an initializer list as if the implementation allocated an array of N elements of type E, where N is the number of elements in the initializer list. Each element of that array is copy-initialized with the corresponding element of the initializer list, and the std::initializer_list<E> object is constructed to refer to that array. If a narrowing conversion is required to initialize any of the elements, the program is ill-formed.

    6.- The lifetime of the array is the same as that of the initializer_list object.


    So for your particular case, the implementation would be roughly equivalent to this:

    std::initializer_list<std::function<std::string()>> getInitializer() {
      std::function<std::string()> __a[1] = {
        []() -> std::string {
          return "If";
        }
      };
      return std::initializer_list<std::function<std::string()>>(__a, __a+1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a little confused about how to do something that I thought would be
I am a little confused about Accept-Encoding . I have Web Service which would
I am a little confused about how I would restructure my database, currently I
I'm a little confused about CSS and the class attribute. I always thought, the
I'm a little confused about the distinction between mutable and immutable objects. I tried
I am a little confused about when do I have to serialize objects using
I'm a little confused about how ValueType objects are managed memory-wise in .NET. I
I am a little confused about the roles of a java application server and
I'm a little confused about something. I wrote an app and tested it on
I'm a little confused about the HTTP protocol, from what i know HTTP was

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.