English 中文(简体)
Boost MPL: 只有在存在时才能要求(成员)
原标题:Boost MPL: Call a (member) function only if it exists

I have a class A that has a template parameter T. There are use cases where the class T offers a function func1() and there are use cases where T doesn t offer it. A function f() in A should call func1(), iff it exists. I think this should be possible with boost mpl, but I don t know how. Here some pseudo code:

template<class T>
class A
{
    void f(T param)
    {
        if(T::func1 is an existing function)
            param.func1();
    }
};

更糟的是:

template<class T>
class A
{
    void f(T param)
    {
        if(T::func1 is an existing function)
            param.func1();
        else
            cout << "func1 doesn t exist" << endl;
    }
};
最佳回答

Boost.MPL doesn t deal with that as it s strictly for TMP and you can t call members in TMP. Boost.Fusion and Boost.TypeTraits don t have anything either; I thought one of them would but apparently I m misremembering.

https://stackoverflow.com/questions/257288/is-it-possible-to-write-a-c-template-to-check-for-a-Functions- There/264088#264088>Herehere<> 关于如何在C++03中写一个海峡以检测一名成员,有些解决办法。 一旦具备这种特性(请上has_func1_member),你可以将其用于SFINAE:

template<typename T>
typename boost::enable_if<has_func1_member<T> >::type
maybe_call(T& t)
{ t.func1(); }

template<typename T>
typename boost::disable_if<has_func1_member<T> >::type
maybe_call(T&)
{
    // handle missing member case
}

// your example code would then do:
maybe_call(param);

请注意,C++11虽然仍然有一些弧度,但首先更容易书写海峡。

问题回答

暂无回答




相关问题
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?

热门标签