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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:52:52+00:00 2026-05-13T07:52:52+00:00

I am trying to specialize some utility code on const member functions, but have

  • 0

I am trying to specialize some utility code on const member functions, but have problems to get a simple test-case to work.
To simplify the work i am utilizing Boost.FunctionTypes and its components<FunctionType> template – a MPL sequence which should contain the tag const_qualified for const member functions.

But using the test-code below, the specialization on const member functions fails. Does anybody know how to make it work?

The test-code prints out (using VC8 and boost 1.40):

non-const
non-const

Expected output is:

non-const
const

The test-code itself:

#include <iostream>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/function_types/function_type.hpp>
#include <boost/mpl/contains.hpp>

namespace ft  = boost::function_types;
namespace mpl = boost::mpl;

template<typename F>
struct select 
{    
    template<bool IsConst /* =false */>
    struct helper {
        static void f() { std::cout << "non-const" << std::endl; }  
    };

    template<>
    struct helper</* IsConst= */ true> {
        static void f() { std::cout << "const" << std::endl; }  
    };

    typedef ft::components<F> components;
    typedef typename mpl::contains<components, ft::const_qualified>::type const_qualified;
    typedef helper<const_qualified::value> result;
};

typedef boost::function<void (void)> Functor;

template<typename MF>
Functor f(MF f)
{
    return boost::bind(&select<MF>::result::f);
}

class C 
{
public:
    void f1() {}
    void f2() const {}
};

int main()
{
    f(&C::f1)(); // prints "non-const" as expected
    f(&C::f2)(); // prints "non-const", expected "const"
}
  • 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-05-13T07:52:53+00:00Added an answer on May 13, 2026 at 7:52 am

    While its still unclear to me why the approach via function_types::components<> doesn’t work, i realized that there is a simpler approach with Boost.FunctionTypes to specialize on const member functions:
    The classification meta functions like is_member_function_pointer<> optionally take a tag parameter …

    template<typename F>
    struct select 
    {    
        /* ... helper-struct as before */
    
        typedef ft::is_member_function_pointer<F, ft::const_qualified> const_qualified;
        typedef helper<const_qualified::value> result;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've run into some problems when trying to serialize my object to XML. The
I have an XML file that I'm trying to serialize into an object. Some
I have some XML that I am trying serialize like so: string Value =
I'm trying to post some form data and return results but I am having
I am trying to make some data from database available using XML, I have
I am trying to port some C++ code from Windows to Solaris(Unix). There are
I'm trying to serialize some objects with protobuf-net , but unfortunately they make liberal
I'm trying to serialize some relatively simple models into json. For example, I'd like
I am trying to serialize some Linq objects using this code. private byte[] GetSerializedObj(object
I am trying to serialize an object but I am facing some issues regarding

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.