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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:29:03+00:00 2026-06-01T04:29:03+00:00

While developing an application, I had the following problem. I wanted to return an

  • 0

While developing an application, I had the following problem. I wanted to return an empty std::list<string> when a given function pointer was null, or the result of that function otherwise. This is a simplified version of my code:

typedef std::list<std::string> (*ParamGenerator)();

std::list<std::string> foo() {
    /* ... */
    ParamGenerator generator = ...;
    if(generator)
        return generator();
    else
        return {};
}

However, I usually like to use the ternary (?:) operator in these cases, so I tried using it this way (as usual):

return generator ? generator() : {};

But got this error:

somefile.cpp:143:46: error: expected primary-expression before ‘{’ token
somefile.cpp:143:46: error: expected ‘;’ before ‘{’ token

Does this mean I can’t use the ternary operator to return objects created using their constructor from an initializer_list? Is there any particular reason for that?

  • 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-01T04:29:04+00:00Added an answer on June 1, 2026 at 4:29 am

    Standard writes in 8.5.4.1: List-initialization

    Note: List-initialization can be used

    • as the initializer in a variable definition (8.5)
    • as the initializer in a new expression (5.3.4)
    • in a return statement (6.6.3)
    • as a function argument (5.2.2)
    • as a subscript (5.2.1)
    • as an argument to a constructor invocation (8.5, 5.2.3)
    • as an initializer for a non-static data member (9.2)
    • in a mem-initializer (12.6.2)
    • on the right-hand side of an assignment (5.17)

    Nothing of them is a ternary operator. The more minimalistic return 1?{}:{}; is invalid too, what you want is impossible.

    Of course you can explicitly call the constructor std::list<std::string>{}, but I would recommend to write out the if–else-block as you already did.

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

Sidebar

Related Questions

While developing my application i came across some comparison stuff here was it: string
While developing a C++ application, I had to use a third-party library which produced
I've encountered an interesting problem while developing for our legacy XWindows application. For reasons
I'm developing applications for android devices and had a problem while developing lately. I
I have a small problem while developing an application. I want to access all
I am developing an Android application where I get the following exception while writing
While developing a WinForms application, I came across what I believe is a bug
While developing my WordPress application i have a requirement where i need to show
While developing a Launcher (Homescreen) application for Android, I've come into a security exception
I'm having an issue while developing a basic Rails application. I've implemented Twitter Bootstrap

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.