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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:31:47+00:00 2026-06-19T01:31:47+00:00

I am still struggling with some C++ syntax. This time I want to add

  • 0

I am still struggling with some C++ syntax.
This time I want to add extra arguments with the lambda. But to make the code generic I ant to be able to accept any function and its arguments:

#include <functional>
#include <exception> 

template<typename R>
class Nisse
{
    private:
        Nisse(Nisse const&)             = delete;
        Nisse(Nisse&&)                  = delete;
        Nisse& operator=(Nisse const&)  = delete;
        Nisse& operator=(Nisse&&)       = delete;
    public:
        //Nisse(std::function<R()> const& func) {}  // disable for testing

        template<typename... Args>
        Nisse(std::function<R(Args...)> const& func, Args... a) {}
};

int main()
{
    // I  was hoping this would deduce the template arguments.
    Nisse<int>   nisse([](int a,double d){return 5;},12,12.0);
}

This generates:

> g++ -std=c++0x Test.cpp 
Test.cpp:21:61: error: no matching function for call to ‘Nisse<int>::Nisse(main()::<lambda(int, double)>, int, double)’
Test.cpp:21:61: note: candidate is:
Test.cpp:16:9: note: template<class ... Args> Nisse::Nisse(const std::function<R(Args ...)>&, Args ...)

I tried explicitly specifying the template types:

    Nisse<int>   nisse<int,double>([](int a,double d){return 5;},12,12.0);

But this (surprising to me) is a syntax error:

> g++ -std=c++0x Test.cpp 
Test.cpp: In function ‘int main()’:
Test.cpp:21:23: error: expected initializer before ‘<’ token
Test.cpp:21:65: error: expected primary-expression before ‘,’ token
Test.cpp:21:73: error: expected ‘;’ before ‘)’ token
  • 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-19T01:31:48+00:00Added an answer on June 19, 2026 at 1:31 am

    You can’t infer std::function template arguments from a lambda. The usual way to accept arbitrary callables is by universal reference:

        template<typename F, typename... Args,
          typename = typename std::enable_if<std::is_convertible<
            decltype(std::declval<F>()(std::declval<Args>()...)), R>::value>::type>
        Nisse(F &&f, Args... a): Nisse(std::function<R()>(std::bind(f, a...))) {}
    

    The final (anonymous, defaulted) template argument is used here to validate that the first template argument is function like and returns a result of type R.

    std::enable_if<std::is_convertible<
        decltype(std::declval<F>()(std::declval<Args>()...)), R>::value>::type
    

    As described in the comments below by @Yakk The above expression checks that F is a function type whose result is R. If it works then all is fine. If it fails then it generates a compile time error (NOTE: this uses SFINAE).

    For methods, SFINAE is inserted into the return type, but for constructors this isn’t an option; historically an extra, default constructor argument was added but adding a defaulted template argument is more elegant as it doesn’t change the constructor signature at all. SFINAE by anonymous template parameter is particularly attractive following variadic template parameters as there’s no way for the user to (accidentally?) override the default.

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

Sidebar

Related Questions

Apologies about this simple question. Still struggling with some of the memory concepts here.
I've looked through some posts around here, but am still struggling to get my
I'm still struggling to find an answer to my question. I want to download
I am trying learn the use of lambda expressions and hence still struggling to
I have been struggling with this yesterday whole day, and still couldnt figure it
This is a real newbie question, but I am struggling to get this to
I'm very new to Android development and am still struggling with some really basic
I've done some reading about jQuery noConflict mode, and am still struggling with it
I'm new to programming in Cocoa, so I'm still struggling to grasp some basic
I am making progress but still struggling with Unit of Work in a multi

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.