English 中文(简体)
3. 返回点最好从超负荷装置上匹配,无须打电话
原标题:Retrieve pointer to best match from overload set without calling

原因 我需要使用2个阶段的施工,此外,最后阶段由另一个线索推迟和进行,有些情况是:

...
#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()
...

这里的问题是,我想从为连接Type设定的超负荷中挑选出最好的匹配:Init,但投射物是不同的,即使某些论点是可兑换的,也找不到完美的匹配。 因此,问题是: 能否获得这种类型和组件;在未实际使用超载体时,点击器达到最佳匹配水平? 不能使用C++03中可用吨数。

最佳回答

You can leverage lazy evaluation expression templates.

AFAIK约束性言论正是在这个家庭中(如“温斯特角”、“灵氏度”等)。

<上<>最后>我共同行动。 然而,它只使用可移动物体,有<>超载操作员<>。 我恳请你们能够像这 gl一样使用某种东西。

  • 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)

这里是C++11 demo(低于)部分港口,进入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)

作为一个简单的例子,这应当做到:

#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
}
问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签