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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:51:42+00:00 2026-06-12T23:51:42+00:00

I am assigning to a std::function<double()> a lambda expression. This snippet works if(fn_type==exponential) k.*variable

  • 0

I am assigning to a std::function<double()> a lambda expression. This snippet works

if(fn_type==exponential)
    k.*variable = [=,&k](){ return initial*exp(-k.kstep*par); };
else
    k.*variable = [=,&k](){ return initial*pow(k.kstep, par); };

whereas if I want to use the ternary operator

k.*variable = (fn_type==exponential ? [=,&k](){ return initial*exp(-k.kstep*par); } : [=,&k](){ return initial*pow(k.kstep, par); });

I get the following error:

error: no match for ternary ‘operator?:’ in <awfully long template error, because this whole thing is in a class defined in a function...>

Is this a gcc bug (I’m using 4.7.2)? Otherwise why is there this limit in the standard?

  • 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-12T23:51:43+00:00Added an answer on June 12, 2026 at 11:51 pm

    The second and third operands of the conditional operator must have the same type or there must be some common type to which they can both be converted that the compiler can figure out. There are only a handful of conversions that the compiler will consider.

    Your two lambda expressions have different types, and there is no common type to which they can both be converted (conversions to user-defined types, like std::function<double()>, cannot be considered because there are potentially an infinite number of valid target types).

    You can directly convert each of the operands to std::function<double()>:

    k.*variable = fn_type==exponential
        ? std::function<double()>([=,&k](){ return initial*exp(-k.kstep*par); })
        : std::function<double()>([=,&k](){ return initial*pow(k.kstep, par); });
    

    But really, it’s cleaner with the if/else.

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

Sidebar

Related Questions

Assigning values without using usual notation like this.<Double>getAnything(int flag) private <T> T getAnything(int flag)
Why assign a function to a variable? What's the point in assigning int x,
Assigning a Date variable to another one will copy the reference to the same
When assigning a type casted array to codeigniter's session, I get this error: A
I am assigning to an ImageView contacts images using this code: mPhotoView = (ImageView)
Possible Duplicate: Explicitly assigning values to a 2D Array? sorry if this has been
We are assigning new key codes to an onKeyPress event in IE with this
One of our classes provides tr1::function callback object. When I try assigning a member
I needed a function to return a string. I used the following code to
Given this code: #include <cstdio> #include <iostream> #include <string> using std::cin; using std::cout; using

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.