gpt4 book ai didi

C++:使用函数指针作为模板参数

转载 作者:行者123 更新时间:2023-11-30 04:37:37 25 4
gpt4 key购买 nike

我有以下代码:

template<typename Parent, typename T, void (Parent::*Setter)(T), T (Parent::*Getter)()>
struct Property {
Parent& obj;
Property(Parent& _obj) : obj(_obj) {}
Property& operator=(T v) { (obj.*Setter)(v); return *this; }
operator T() { return (obj.*Getter)(); }
};

template<typename T1, typename T2>
class Bimap {
public:
class Entry;
typedef std::tr1::shared_ptr<Entry> EntryP;
typedef std::multimap<T1, EntryP> T1Map;

class Entry {
private:
Bimap& bimap;
typename T1Map::iterator it1;

void set1(T1 v) { bimap.map1.erase(it1); it1 = bimap.map1.insert(typename T1Map::value_type(v, it2->second)).first; }
T1 get1() { return it1->first; }
public:
Property<Entry,T1,set1,get1> first() { return Property<Entry,T1,set1,get1>(*this); }
};
};

这段代码给我(在 Property<...> first() 上):

'void Bimap<T1, T2>::Entry::set1(T1) [with T1 = double, T2 = std::tr1::shared_ptr<Node>]' cannot appear in a constant-expression

我也试过

Property<Entry,T1,&set1,&get1> first() { return Property<Entry,T1,&set1,&get1>(*this); }

但这给了我:

error: wrong number of template arguments (3, should be 4)
error: provided for 'template<class Parent, class T, void (Parent::* Setter)(T), T (Parent::* Getter)()> struct Property'

有没有可能以某种方式解决这个问题?或者为什么那是不可能的?

最佳答案

你想要

Property<Entry, T1, &Entry::set1, &Entry::get1>

关于C++:使用函数指针作为模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3746728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com