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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:35:26+00:00 2026-06-18T12:35:26+00:00

I have a boost::variant and I would like to execute a functor only if

  • 0

I have a boost::variant and I would like to execute a functor only if the variant is of a special type, so I made up this function:

template<typename T, typename Variant>
void if_init(Variant& opt_variant, std::function<void(T)> functor){
    if(auto* ptr = boost::get<T>(&opt_variant)){
        functor(*ptr);
    }   
}

This works well, but I would like the type T to be deduced, so that I can write that:

if_init(b, [](double var){ std::cout << "I'm double and set" << std::endl; });

But the type is not deduced:

type_inference.cpp:19:5: error: no matching function for call to 'if_init'
    if_init(b, [](double var){ std::cout << "I'm double and set" << std::endl; }); 
    ^~~~~~~
type_inference.cpp:10:6: note: candidate template ignored: failed template argument deduction
    void if_init(Variant& opt_variant, std::function<void(T)> functor){

If I write:

if_init<double>(b, [](double var){ std::cout << "I'm double and set" << std::endl; });

it works well.

Is there a way to have type T being deduced ? I would like to type T only once. Here the type is short, but in the real case, there are long types.

I’m using CLang 3.2.

Here is the full test case (the first call compiles not the second):

#include <iostream>
#include <functional>
#include <boost/variant.hpp>

typedef boost::variant<int, double> Test;

template<typename T, typename Variant>
void if_init(Variant& opt_variant, std::function<void(T)> functor){
    if(auto* ptr = boost::get<T>(&opt_variant)){
        functor(*ptr);
    }   
}

int main(){
    Test b = 1.44; 

    if_init<double>(b, [](double var){ std::cout << "I'm double and set" << std::endl; });
    if_init(b, [](int var){ std::cout << "I'm int and set" << std::endl; });      

    return 0;
}
  • 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-18T12:35:27+00:00Added an answer on June 18, 2026 at 12:35 pm

    I recommend you think of std::function<Sig> as a container of any one functor that conforms to Sig as a signature — and which can be replaced at any moment. This functionality comes in very handy for e.g. std::vector<std::function<Sig>> because such a container can then hold functors of different types.

    In your case, because you only care to have just the one functor you really don’t need the functionality of std::function<Sig>. As such, I recommend you declare your function template like so:

    template<typename T, typename Variant, typename Functor>
    void if_init(Variant& opt_variant, Functor functor);
    

    If you are worried that this doesn’t communicate that Functor must conform to a void(T) signature, please note that std::function<Sig> does not enforce that either: although obviously you will end up with a compilation error, it is not a nice one. It’s planned to be changed (and maybe your implementation has that either), but changed to a different kind of error. Still not that helpful for your case.

    I personally make use of template aliases (in the template parameter list) to both document and enforce what a functor should conform to. This ends up looking like:

    // Documents that e.g. long l = std::forward<Functor>(functor)(42.)
    // should be a valid expression -- a functor that returns int would
    // also be accepted.
    // Triggers a hard-error (typically a static_assert with a nice message)
    // on violation.
    template<typename Functor, Requires<is_callable<Functor, long(double)>>...>
    R foo(Functor functor);
    
    // Documents that this function template only participates in overload resolution
    // if the functor conforms to the signature.
    // Does not trigger a hard-error (necessary by design); if everything goes right
    // then another overload should be picked up -- otherwise an error of the kind
    // 'no matching overload found' is produced
    template<typename Functor, EnableIf<is_callable<Functor, long(double)>>...>
    R bar(Functor functor);
    

    As to your exact question, the rules of C++ do not allow for a template parameter to be deduced in your case. It’s really not an easily fixed ‘problem’, if it is one. You can find more information on this.

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

Sidebar

Related Questions

I have a boost variant of looking like this: typedef boost::variant<int, float, double, long,
I have a boost::posix_time::ptime that points to March 31st 2010 like this: ptime p(date(2010,
Let's say that I have a boost::variant<std::string, int> myVariant; In this object I keep
In my C++ library I have a type boost::variant<A,B> and lots of algorithms getting
I have made a program using boost::variant that somehow isn't const correct. error: passing
I have a boost::posix_time::ptime instance and would like to convert (format) it to a
So I have a boost::multi_index_container with multiple non-unique indexes. I would like to find
I have a template function that I wish to return either the type of
http://www.boost.org/doc/libs/1_35_0/doc/html/boost/get_id405862.html template<typename U, typename T1, typename T2, ..., typename TN> U & get(variant<T1, T2,
I have these variables: boost::regex re //regular expression to use std::string stringToChange //replace this

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.