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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:55:46+00:00 2026-05-27T02:55:46+00:00

For various reasons I need to use 2 phase construction, furthermore the last phase

  • 0

For various reasons I need to use 2 phase construction, furthermore the last phase is deferred and performed by another thread, some context:

...
#define BOOST_PP_LOCAL_MACRO(n) \
        template < typename ConnectionType, BOOST_PP_ENUM_PARAMS(n, typename T) > \
        boost::shared_ptr< ConnectionType > Connect( BOOST_PP_ENUM_BINARY_PARAMS(n, T, arg) ) \
        { \
            boost::shared_ptr< ConnectionType > con( boost::make_shared< ConnectionType >() ); \
            boost::mutex::scoped_lock sl( m_AddNetworkJobMutex ); \
            m_NetworkJobs.push_back( boost::bind( static_cast< void ( ConnectionType::* )( BOOST_PP_ENUM_PARAMS(n,T) ) >( &ConnectionType::Init ), con, BOOST_PP_ENUM_PARAMS(n, arg) ) ); \
            return con; \
        } 

#define BOOST_PP_LOCAL_LIMITS (1, 5)
#include BOOST_PP_LOCAL_ITERATE()
...

The problem here is that I want to select the best match possible from the overload set for ConnectionType::Init, but the cast is distinct and can’t find a perfect match even if some of the arguments are convertible. So the questions becomes: Is it possible to somehow get the type & pointer to the best match from the overload set without actually calling it? Can’t use anything which isn’t available in C++03.

  • 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-27T02:55:47+00:00Added an answer on May 27, 2026 at 2:55 am

    You can leverage lazy evaluation expression templates.

    AFAIK bind expressions are precisely in that family (as are Boost Proto epxressions, Spirit Grammar parse expressions etc.).

    Update finally got my act together. However, it only works with callable objects with overloaded operator(). I suppose you can use something like this as glue?

    • I now show both C++03 and C++11 proofs-of-concept that might help get something of a glue functor built-up around this
    • The C++03 is near equivalent (see the // TODO in the code)
    • The C++03 version depends on Boost Typeof and Boost Bind (see Boost Utility doc for result_of for backgrounds on result types of polymorphic function objects)
    • Both versions live on IdeOne

    C++03 (live on https://ideone.com/VHcEC)

    Here is a partial port of the C++11 demo (below) into C++03 + Boost:

    #include <string>
    #include <iostream>
    #include <boost/bind.hpp>
    #include <boost/typeof/typeof.hpp>
    
    struct overloaded
    {
        typedef int result_type;
    
        int operator()(const std::string& s) const { return 1; }
        int operator()(double d)             const { return 2; }
    };
    
    struct factory
    {
        template <typename T> struct result { typedef BOOST_TYPEOF_TPL(boost::bind(overloaded(), T())) type; };
    
        template <typename T>
        typename result<T>::type operator()(const T& t) const 
        {
            return boost::bind(overloaded(), t);
        }
    };
    
    int main()
    {
        overloaded foo;
    
        // based on local bind expression:
        BOOST_AUTO(unresolved, boost::bind(foo, _1));
        std::cout << unresolved("3.14") << std::endl;  // should print 1
        std::cout << unresolved(3.14)   << std::endl;  // should print 2
    
        // based on a factory function template
        factory makefoo;
        std::string str("3.14"); // TODO get rid of this explicit instanciation?
        std::cout << makefoo(str)() << std::endl;  // should print 1
        std::cout << makefoo(3.14)()   << std::endl;  // should print 2
    }
    

    C++11 (live on https://ideone.com/JILEA)

    As a simple example, this should work alright:

    #include <string>
    #include <iostream>
    #include <functional>
    
    using namespace std::placeholders;
    
    struct overloaded
    {
        int operator()(const std::string& s) const { return 1; }
        int operator()(double d)             const { return 2; }
    };
    
    template <typename T>
    auto makefoo(const T& t) -> decltype(std::bind(overloaded(), t))
    {
        return std::bind(overloaded(), t);
    }
    
    int main()
    {
        overloaded foo;
    
        // based on local bind expression:
        auto unresolved = std::bind(foo, _1);
        std::cout << unresolved(3.14)   << std::endl;  // should print 2
        std::cout << unresolved("3.14") << std::endl;  // should print 1
    
        // based on a factory function template
        std::cout << makefoo(3.14)()   << std::endl;  // should print 2
        std::cout << makefoo("3.14")() << std::endl;  // should print 1
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For various reasons, I need to use the raw SpeechRecognizer API instead of the
For various reasons I need to use AVPlayer to play music from the IOS
For various reasons (code review mostly) I need to switch from current development branch
For various reasons, I need to runOnUiThread() the actual instantiation & initialization of WebView
So I've read some articles about scaling Socket.IO. For various reasons I don't want
For various reasons I have decided to use surrogate keys (in the form of
I have been looking for an alternative to Hibernate for various reasons. I came
Using get/set seems to be a common practice in Java (for various reasons), but
I am creating a GTK Window which, for various reasons, I wish to make
I have a Yahoo! Group that has its own Database tables for various reasons.

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.