Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有混合参数类型的模板重载 我正在研究C++中的一个属性系统(C++ 11也可以),这样类就可以向脚本系统提供数据。我想提供各种“类型”的属性,最简单的一种在内部保持其价值,如下所示: // Simple Property template<typename Value> class Property { public: Value get() const; void set(Value value); private: Value m_value; } // Complicated Property template< typename Value, typename ValueOwner, Value (ValueOwner::*Getter)(void) const, void (ValueOwner::*Setter)(Value) > class Property { // Stuff for calling the getter & setter } template<typename A, typename B, typename C, typename D> class Property; // Simple Property // ... // Complicated Property // ..._C++_Templates - Fatal编程技术网

具有混合参数类型的模板重载 我正在研究C++中的一个属性系统(C++ 11也可以),这样类就可以向脚本系统提供数据。我想提供各种“类型”的属性,最简单的一种在内部保持其价值,如下所示: // Simple Property template<typename Value> class Property { public: Value get() const; void set(Value value); private: Value m_value; } // Complicated Property template< typename Value, typename ValueOwner, Value (ValueOwner::*Getter)(void) const, void (ValueOwner::*Setter)(Value) > class Property { // Stuff for calling the getter & setter } template<typename A, typename B, typename C, typename D> class Property; // Simple Property // ... // Complicated Property // ...

具有混合参数类型的模板重载 我正在研究C++中的一个属性系统(C++ 11也可以),这样类就可以向脚本系统提供数据。我想提供各种“类型”的属性,最简单的一种在内部保持其价值,如下所示: // Simple Property template<typename Value> class Property { public: Value get() const; void set(Value value); private: Value m_value; } // Complicated Property template< typename Value, typename ValueOwner, Value (ValueOwner::*Getter)(void) const, void (ValueOwner::*Setter)(Value) > class Property { // Stuff for calling the getter & setter } template<typename A, typename B, typename C, typename D> class Property; // Simple Property // ... // Complicated Property // ...,c++,templates,C++,Templates,但这也不起作用,因为Getter/Setter模板不希望typename作为第三个和第四个参数,而是指向成员函数的指针 下一次尝试: // Complicated Property // ... // Simple Property template<typename Value> class Property< Value, Property<Value>, &Property<Value>::get,

但这也不起作用,因为Getter/Setter模板不希望typename作为第三个和第四个参数,而是指向成员函数的指针

下一次尝试:

// Complicated Property
// ...

// Simple Property
template<typename Value>
class Property<
    Value, 
    Property<Value>, 
    &Property<Value>::get, 
    &Property<Value>::set
>;

让编译器弄清楚我指的是哪个模板。

是否需要在编译时指定getter和setter?您可以使用
值所有者
模板参数获取类型:

template<
    typename Value, 
    typename ValueOwner, 
>
class Property {
public:
    typedef Value (ValueOwner::*Getter)(void) const;
    typedef void (ValueOwner::*Setter)(Value);

    Property(Getter getter, Setter setter)
        : getter(getter), setter(setter) {}

    // ...

private:
    Getter getter;
    Setter setter;
};
模板<
typename值,
typename ValueOwner,
>
类属性{
公众:
typedef值(ValueOwner::*Getter)(void)const;
typedef void(值所有者::*Setter)(值);
属性(Getter-Getter,Setter-Setter)
:getter(getter),setter(setter){}
// ...
私人:
吸气剂;
二传手二传手;
};
然后将适当的函数指针传递给构造函数:

Property<int, MyClass> complicatedProperty(&MyClass::get, &MyClass::set);
Property属性(&MyClass::get,&MyClass::set);
但我很好奇是否有一个解决方案,使[…]

是的,有

您可以使用以下方法。首先,在客户端不应处理的某些命名空间中创建一个伪类模板:

namespace detail
{
    template<typename T>
    struct dummy
    {
        T get() const { return T(); }
        void set(T) { }
    };
}
最后,您可以使用您提出的两个声明:

struct MyClass
{
    int get() const { return 42; }
    void set(int) { }
};

int main()
{
    Property<int> simpleProperty;
    Property<int, MyClass, &MyClass::get, &MyClass::set> complicatedProperty;
}
struct MyClass
{
int get()常量{return 42;}
空集(int){}
};
int main()
{
财产单一性;
财产性;
}
这是一个.

好的,它们不必在编译时指定,但最好利用内联。我将不得不测试gcc是否足够聪明,能够将这些调用与您的解决方案内联。无论如何,谢谢!
Property<int, MyClass> complicatedProperty(&MyClass::get, &MyClass::set);
namespace detail
{
    template<typename T>
    struct dummy
    {
        T get() const { return T(); }
        void set(T) { }
    };
}
#include <type_traits>

// Simple Property
template<typename Value,
         typename ValueOwner = detail::dummy<Value>,
         Value (ValueOwner::*Getter)(void) const = &ValueOwner::get,
         void (ValueOwner::*Setter)(Value) = &ValueOwner::set,
         bool = std::is_same<ValueOwner, detail::dummy<Value>>::value
         >
class Property {
    public:
        Value get() const;
        void set(Value value);
    private:
        Value m_value;
};
// Complicated Property
template<
    typename Value,
    typename ValueOwner,
    Value (ValueOwner::*Getter)(void) const,
    void (ValueOwner::*Setter)(Value)
>
class Property<Value, ValueOwner, Getter, Setter, false> 
{
  // Stuff for calling the getter & setter
};
struct MyClass
{
    int get() const { return 42; }
    void set(int) { }
};

int main()
{
    Property<int> simpleProperty;
    Property<int, MyClass, &MyClass::get, &MyClass::set> complicatedProperty;
}