English 中文(简体)
我希望有一个模版功能,使成员有点,但我不想通过该类或成员类型。
原标题:I want a template function that takes a pointer to member, but I don t want to have to pass the class type nor the member type

因此,我敲响了我的大脑,试图为一些事情铺路。 我认为,我在这里要看是否有任何想法。 考虑如下:

template <typename S, typename T, T S::* pMember>
bool SortByMember(const S& L, const S& R)
{
    return L.*pMember < R.*pMember;
}

...

struct SomeStruct
{
    int SomeMember;
};

void SomeFunction(void)
{
    GetSortByMember<&SomeStruct::SomeMember>();
}

I would like the function, GetSortByMember, to return a function pointer to the corresponding instantiation of SortByMember. However, I can t think of a way to declare/define GetSortByMember in a way that doesn t require the user to also pass the class type and the member type. This:

GetSortByMember<SomeStruct, int, &SomeStruct::SomeMember>();

is overly verbose and requires me to state the member type. I m sure there s probably a solution in the boost libraries, but I d rather not introduce that dependency to the project I m working on.

我非常怀疑,存在一种解决办法,可产生代谢法中所用的精确合成物一,但也许可以用模板类别或宏观做些什么?

SortBy Members的签名按使用职能点的类别计算,因此可能不会改变。

最佳回答

也许可以更随意地做你想要做的事情,但这项工作使用大型和海合会特定类型。 我不敢肯定,但在新的C++标准中可能有一种可行的方法。

#include <iostream>

template <class P, P p>
class sort_by_member_t;

template <class S, class T, T S::*p>
class sort_by_member_t<T S::*, p> {
public:
    typedef bool (*fn_t)(S const&, S const&);

    static bool fn(S const& L, S const& R)
    {
        return L.*p < R.*p;
    }
};

#define SORT_BY_MEMBER(p) sort_by_member_t<typeof(p), p>::fn;

struct SomeStruct
{
    int SomeMember;
};


int main()
{
    bool (*fp)(SomeStruct const&, SomeStruct const&);
    fp = SORT_BY_MEMBER(&SomeStruct::SomeMember);
    SomeStruct const a = { 1 };
    SomeStruct const b = { 2 };
    std::cerr
        << (void*) fp <<    
        << (*fp)(a, b) <<    
        << (*fp)(b, a) <<    
        <<  
 ;

    return 0;
}
问题回答

你的例子不明确,大概是,你需要用两个论点来说明由此产生的职能? 如果是的话,为什么不使用“跳板”功能,而是通过,例如:

#include <iostream>

struct foo
{
  int bar;
  int getBar() const { return bar; }
};

template <typename S, typename U>
bool SortByMember(const S& L, const S& R, U f)
{
    return (L.*f)()< (R.*f)();
}

int main(void)
{
  foo a = {1};
  foo b = {2};

  std::cout << SortByMember(a, b, &foo::getBar) << std::endl;
}




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

热门标签