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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:30:41+00:00 2026-06-13T21:30:41+00:00

Could somebody please tell me why I am getting compiler error for the last

  • 0

Could somebody please tell me why I am getting compiler error for the last line of the following code?

note: If I remove the following line, my code gets compiled with no error:

appliedEqualityVisitor(compareValue);

Here is the code:

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

using namespace std;
using namespace boost;


template<typename T>
struct CheckOneTypeEquality : public boost::static_visitor<>
{
    T const* value;
    bool operator()( T const& other ) const
    {
        return other == *value;
    }
    template<typename U>
    bool operator()( U const& other ) const
    {
        return false;
    }
    CheckOneTypeEquality( T const& value_ ):value(&value_) {}
};

typedef variant<int, string> MyVariant;
typedef apply_visitor_delayed_t<CheckOneTypeEquality<MyVariant>> AppliedEqualityVisitorType;

int main(int argc, char **argv) 
{
    int testValue = 12;
    CheckOneTypeEquality<MyVariant> equalityVisitor(testValue);

    AppliedEqualityVisitorType appliedEqualityVisitor = apply_visitor(equalityVisitor);

    MyVariant compareValue = 13;
    appliedEqualityVisitor(compareValue); // <<<<< compile error here

    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-13T21:30:43+00:00Added an answer on June 13, 2026 at 9:30 pm

    The problem stems from your visitor class. Boost expects a void operator()(...), instead you provide an operator() that returns something.

    For your pattern to work, you will have to change the visitor, something like:

    template<typename T>
    struct CheckOneTypeEquality : public boost::static_visitor<>
    {
        T const* value;
        mutable bool check;
        void operator()( T const& other ) const
        {
            check = other == *value;
        }
        template<typename U>
        void operator()( U const& other ) const
        {
          check = false;
        }
        CheckOneTypeEquality( T const& value_ ):value(&value_), check(false) {}
    };
    

    Then test the result. Btw. am not sure that your constructor there to which you pass an int is safe. You are not holding the reference, rather pointing to a temporary instance of the variant which is constructed from the int – which may be out of scope.

    EDIT: I think what you are trying to do is misguided, given that boost::variant already implements operator== correctly. For example:

    MyVariant testValue = 12;
    
    MyVariant compareValue = 13;
    MyVariant compareValue2 = 12;
    MyVariant compareValue3 = "12";
    
    std::cout << (compareValue == testValue) << std::endl;
    std::cout << (compareValue2 == testValue) << std::endl;
    std::cout << (compareValue3 == testValue) << std::endl;
    

    Works fine – and I think it is what you are trying to accomplish? You want to test that two variants are equally comparable(?) This will work as long as all the objects in your variant are equally comparable.

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

Sidebar

Related Questions

Could somebody please provide the code to do the following: Assume there is a
could somebody please tell me what the following expressions means: \d{6,8} As far as
could somebody please tell me why this code fails. url is nil after calling
Could somebody please explain or link to a resource that will tell me why
Could somebody please tell me what is Memory Page Out Rate. I have seen
Could somebody please tell me how I can configure iReport 3.5 (nb) to use
could somebody please tell me how implement the hide function using on(). I've tried:
Could somebody please explain the meaning of this error? INFO: Scanning for root resource
Could somebody please explain to me why does this code only print 42 instead
Could somebody please show code which would do this quickly? Assume we get three

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.