Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/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++;巴顿·纳克曼把戏中使用的非模板成员?_C++_Design Patterns_Templates - Fatal编程技术网

C++ 什么是C++;巴顿·纳克曼把戏中使用的非模板成员?

C++ 什么是C++;巴顿·纳克曼把戏中使用的非模板成员?,c++,design-patterns,templates,C++,Design Patterns,Templates,来自维基百科: // A class template to express an equality comparison interface. template<typename T> class equal_comparable { friend bool operator==(T const &a, T const &b) { return a.equal_to(b); } friend bool operator!=(T const &

来自维基百科:

// A class template to express an equality comparison interface.
template<typename T> class equal_comparable
{
    friend bool operator==(T const &a, T const &b) { return  a.equal_to(b); }
    friend bool operator!=(T const &a, T const &b) { return !a.equal_to(b); }
};

class value_type
// Class value_type wants to have == and !=, so it derives from
// equal_comparable with itself as argument (which is the CRTP).
     : private equal_comparable<value_type>
{
public:
    bool equal_to(value_type const& rhs) const; // to be defined
};
//表示相等比较接口的类模板。
模板类相等\u可比
{
友元布尔运算符==(T常量&a,T常量&b){返回a.equal_到(b);}
友元布尔运算符!=(T常数&a,T常数&b){返回!a.等于(b);}
};
类值类型
//类值\u type希望具有==和!=,所以它来自于
//equal_作为参数与自身可比(即CRTP)。
:私人同等
{
公众:
bool equal_to(value_type const&rhs)const;//待定义
};
这应该是可以实现编译时维度分析(检查应用于变量的某些操作是否以可比的数字结束,比如速度与空间/时间相当,但没有加速度)的方法

有谁能解释一下如何,或者至少解释一下什么是非模板成员


感谢

这些实际上是非模板非成员-基本模板中的比较运算符-它们被ADL用于派生类。模板成员类似于:


class C
{
    ...
    template < typename T > void DoGreatStuff( T t ) { ... }
    ...
};

C类
{
...
模板void DoGreatStuff(T T){…}
...
};

这些实际上是非模板非成员-基本模板中的比较运算符-它们被ADL用于派生类。模板成员类似于:


class C
{
    ...
    template < typename T > void DoGreatStuff( T t ) { ... }
    ...
};

C类
{
...
模板void DoGreatStuff(T T){…}
...
};

value\u type
类中实例化
equal\u compariable
会导致编译器生成两个比较函数:

friend bool operator==(value_type const &a, value_type const &b) { return  a.equal_to(b); }
friend bool operator!=(value_type const &a, value_type const &b) { return !a.equal_to(b); }

这些函数是非模板函数,因为它们不依赖于任何模板参数,但它们也是非成员,因为它们被声明为
friend

value\u type
类中实例化
equal\u compariable
会导致编译器生成两个比较函数:

friend bool operator==(value_type const &a, value_type const &b) { return  a.equal_to(b); }
friend bool operator!=(value_type const &a, value_type const &b) { return !a.equal_to(b); }

这些函数是非模板函数,因为它们不依赖于任何模板参数,但它们也是非成员函数,因为它们被声明为
friend

自模式发明以来,语言规则发生了变化,尽管注意不要破坏它。换句话说,据我所知,它仍然有效,但原因与最初不同。我不认为我会在这种模式的基础上尝试维度分析,因为我认为现在有更好的方法

我还认为这个例子太琐碎了,没有什么帮助。如前所述,
equal\u comparable
的实例化导致
运算符==
运算符=用于显示
value\u type
。因为它们是非成员,所以继承是私有的并不重要,在解析调用时,它们仍然可以进行选择。在这个例子中很难看出要点。但是,假设您向
equal\u compariable
添加了一个模板参数和一些其他内容:

template<typename U, typename V> class equal_comparable
{
    friend bool operator==(U const &a, V const &b) { return  a.equal_to(b); }
    friend bool operator!=(U const &a, V const &b) { return !a.equal_to(b); }
};

class some_other_type 
{
    bool equal_to(value_type const& rhs) const;
};

class value_type
: private equal_comparable<value_type>,      // value_type comparable to itself
  private equal_comparable<some_other_type>  // value_type comparable to some_other_type
{
public:
    bool equal_to(value_type const& rhs) const;
    bool equal_to(some_other_type const& rhs) const;
};
模板类相等\u可比
{
friend bool运算符==(U常量&a,V常量&b){返回a.equal_到(b);}
友元布尔运算符!=(U常量&a,V常量&b){返回!a.equal_to(b);}
};
给一些其他类型分类
{
布尔等于(值类型常量和rhs)常量;
};
类值类型
:private equal\u compariable,//value\u type可与自身进行比较
私人同等类型可比//价值类型可比于其他类型
{
公众:
布尔等于(值类型常量和rhs)常量;
布尔等于(某些其他类型常数和rhs)常数;
};

免责声明:我不知道这是否是它应该使用的方式,但我有理由相信它会按照描述的方式工作。

自模式发明以来,语言规则已经发生了变化,尽管注意不要打破它。换句话说,据我所知,它仍然有效,但原因与最初不同。我不认为我会在这种模式的基础上尝试维度分析,因为我认为现在有更好的方法

我还认为这个例子太琐碎了,没有什么帮助。如前所述,
equal\u comparable
的实例化导致
运算符==
运算符=用于显示
value\u type
。因为它们是非成员,所以继承是私有的并不重要,在解析调用时,它们仍然可以进行选择。在这个例子中很难看出要点。但是,假设您向
equal\u compariable
添加了一个模板参数和一些其他内容:

template<typename U, typename V> class equal_comparable
{
    friend bool operator==(U const &a, V const &b) { return  a.equal_to(b); }
    friend bool operator!=(U const &a, V const &b) { return !a.equal_to(b); }
};

class some_other_type 
{
    bool equal_to(value_type const& rhs) const;
};

class value_type
: private equal_comparable<value_type>,      // value_type comparable to itself
  private equal_comparable<some_other_type>  // value_type comparable to some_other_type
{
public:
    bool equal_to(value_type const& rhs) const;
    bool equal_to(some_other_type const& rhs) const;
};
模板类相等\u可比
{
friend bool运算符==(U常量&a,V常量&b){返回a.equal_到(b);}
友元布尔运算符!=(U常量&a,V常量&b){返回!a.equal_to(b);}
};
给一些其他类型分类
{
布尔等于(值类型常量和rhs)常量;
};
类值类型
:private equal\u compariable,//value\u type可与自身进行比较
私人同等类型可比//价值类型可比于其他类型
{
公众:
布尔等于(值类型常量和rhs)常量;
布尔等于(某些其他类型常数和rhs)常数;
};

免责声明:我不知道这是否是应该使用的方式,但我有理由相信它会按照描述的方式工作。

谢谢,这有助于我理解它。谢谢,这有助于我理解它。