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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:45:09+00:00 2026-06-14T03:45:09+00:00

I am witnessing a behavior in the following code that I don’t understand. The

  • 0

I am witnessing a behavior in the following code that I don’t understand. The point is that if I declare the second overload of operator() like either of the following:

bool operator()(T other) const
bool operator()(const T &other) const

The output of program is:

string

But if I use the following declaration:

bool operator()(T &other) const

The output will be:

other type

Can someone please explain why operator()(const string &other) is not being called in the latter case?

#include "boost/variant/variant.hpp"
#include "boost/variant/apply_visitor.hpp"

using namespace std;
using namespace boost;

typedef variant<string, int> MyVariant;


class StartsWith
    : public boost::static_visitor<bool>
{
public:
    string mPrefix;
    bool operator()(const string &other) const
    {
        cout << "string" << endl;
        return other.compare(0, mPrefix.length(), mPrefix) == 0;
    }
    template<typename T>
    bool operator()(T &other) const
    {
        cout << "other type" << endl;
        return false;
    }
    StartsWith(string const& prefix):mPrefix(prefix){}
};

int main(int argc, char **argv) 
{
    MyVariant v(string("123456"));
    apply_visitor(StartsWith("123"), v);
    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-14T03:45:11+00:00Added an answer on June 14, 2026 at 3:45 am

    You have const problem here.

    You are passing not const object to apply_visitor – so not const object members are passed to applied visitor. So in your case it is string& – reference to string type. This template is exact match for it:

    template<typename T>
    bool operator()(T &other) const
    

    So it is selected. This function is not exact match – it is skipped:

    bool operator()(const string &other) const
    

    Of course if you provide that operator:

    bool operator()(string &other) const
    

    then it would be selected, since non template function are considered before template one.

    So solution is: either provide method in your visitor which takes string reference (not const) – or pass const variant to apply…

    First solution – remove const from string operator:

    bool operator()(/*const*/ string &other) const
    //              ^^^^^^^^^ remove it
    

    Second solution – pass const object:

    const MyVariant& cv = v;
    apply_visitor(StartsWith("123"), cv);
    //                               ^^ const object passed here
    

    Third solution – add const specifier to general visitor:

    template<typename T>
    bool operator()(const T &other) const
    //              ^^^^^ 
    

    Solutions 1st and 3rd are the better than 2nd – you should pass consistent visitor to your variant, const has strong meaning when compiler has to select appropriate function.

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

Sidebar

Related Questions

I'm witnessing some strange behavior with SharePoint when changing user's Active Directory passwords. I
Would someone please take a look at the following code fragments (all from one
I'm witnessing a strange behavior in a .net program : Console.WriteLine(Int64.MaxValue.ToString()); // displays 9223372036854775807,
I have been witnessing this weird behavior since last night. Here's the fiddle: http://jsfiddle.net/Gqndm/
The following piece of C# code does not compile: public class A { public
I currently am witnessing an error that only happens on Release mode of my
I'm witnessing some strange behaviour from Fluent nHibernate. I have two objects involved in
UPDATE: I've modified the code to drop the indexes before the inserting, but it
Of course, we have been witnessing a lot of browsers differences in rendering elements,
(See this question in ServerFault ) I have a Java client that uses Socket

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.