使用std :: enable_if作为templ时的默认模板参数。 param:为什么只有在enable_if参数不同的两个模板函数? [英] Default template argument when using std::enable_if as templ. param.: why OK with two template functions that differ only in the enable_if parameter?

查看:228
本文介绍了使用std :: enable_if作为templ时的默认模板参数。 param:为什么只有在enable_if参数不同的两个模板函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std :: enable_if 的语言参考中cppreference



$ b

一个常见的错误是声明两个不同于
的函数模板,只有它们的默认模板参数。这是非法的,因为
默认模板参数不是函数的一部分模板的
签名,并用
相同的签名声明两个不同的函数模板是非法的。


函数在下面的例子,在我看来,这种情况发生。也就是说,两个模板函数 onlyForDerivedObjects(...)似乎(对我)只有它们的默认模板参数不同。我意识到我在这里缺少了一些东西,希望有人可以向我解释这个问题,或指出我可以找到一个突然显现为我自己的方向。




  • 问题: Wrt上面的引用,为什么下面的例子编译和运行罚分:我把mclassify typename std :: enable_if ... 部分在下面的模板函数,当我认为它产生具有两个模板函数的情况,这两个模板函数仅在它们的默认模板参数方面不同。



示例





基类和派生类:

  class BaseA 
{
public:
int getInt()const {return 21; };
};

class DerivedA:public BaseA {};

class BaseB
{
public:
int getAnotherInt()const {return 33; };
};

class DerivedB:public BaseB {};

使用以下模板函数

  / *模板函数,似乎只有不同的
默认模板参数? * /
template< class T,
typename std :: enable_if< std :: is_base_of< BaseA,T> :: value> :: type * = nullptr>
int onlyForDerivedObjects(const T& obj)
{
return 2 * obj.getInt();
}

template< class T,
typename std :: enable_if< std :: is_base_of< BaseB,T> :: value> :: type * = nullptr>
int onlyForDerivedObjects(const T& obj)
{
return 3 * obj.getAnotherInt();
}

编译并运行正常( g ++ -Wall -std = c ++ 11 ... g ++ 4.9.3

  #include< iostream> 
#include< type_traits>

/ * ...上面的类和模板函数* /

/ *模板参数扣除似乎工作正常* /
int main()
{
DerivedA * objA = new DerivedA();
DerivedB * objB = new DerivedB();

std :: cout<< onlyForDerivedObjects(* objA)<< std :: endl; // 42
std :: cout<< onlyForDerivedObjects(* objB)<< std :: endl; // 99

return 0;
}






解决方案 div>


注释



一个常见的错误是声明两个函数模板,模板参数。这是非法的,因为默认模板参数不是函数模板签名的一部分,并且声明具有相同签名的两个不同的函数模板是非法的。




在这两种情况下,默认模板的默认模板参数不同, nullptr ,但第二个模板参数在每种情况下都不同。


In the language reference of std::enable_if at cppreference the following note is included

Notes

A common mistake is to declare two function templates that differ only in their default template arguments. This is illegal because default template arguments are not part of function template's signature, and declaring two different function templates with the same signature is illegal.

In the template functions in example below, it seems to me that this situation occurs. I.e., the two template functions onlyForDerivedObjects(...) seem (to me) to differ only by their default template arguments. I realize I am missing something here, and hopefully someone can explain this to me, or point me in the direction to where I might find an epiphany for myself.

  • Question: W.r.t. the quote above, why do the example below compile and run fine: do I misclassify the typename std::enable_if ... part in the template functions below when I consider it to yield a situation with two template functions which differ only in their default template argument?

Example

Base and derived classes:

class BaseA
{
public:
  int getInt() const { return 21; };
};

class DerivedA : public BaseA {};

class BaseB
{
public:
  int getAnotherInt() const { return 33; };
};

class DerivedB : public BaseB {};

with the following template functions

/* template functions that, seemingly, only differ in their
   default template arguments? */
template< class T,
          typename std::enable_if<std::is_base_of<BaseA, T>::value>::type* = nullptr >
int onlyForDerivedObjects(const T& obj)
{
  return 2*obj.getInt();
}

template< class T,
          typename std::enable_if<std::is_base_of<BaseB, T>::value>::type* = nullptr >
int onlyForDerivedObjects(const T& obj)
{
  return 3*obj.getAnotherInt();
}

compiles and runs fine (g++ -Wall -std=c++11 ..., g++ 4.9.3)

#include <iostream>
#include <type_traits>

/* ... classes and template functions as above */

/* template argument deduction seems to work fine */
int main()
{
  DerivedA* objA = new DerivedA();
  DerivedB* objB = new DerivedB();

  std::cout << onlyForDerivedObjects(*objA) << std::endl; // 42
  std::cout << onlyForDerivedObjects(*objB) << std::endl; // 99

  return 0;
}


(Feel free to edit the title of this post to something possibly more appropriate; I really couldn't come up with anything better myself)

解决方案

Notes

A common mistake is to declare two function templates that differ only in their default template arguments. This is illegal because default template arguments are not part of function template's signature, and declaring two different function templates with the same signature is illegal.

Your functions don't differ only in their default template arguments, they differ in their template parameters, so have different signatures.

In both cases the default template argument is nullptr, but the second template parameter is different in each case.

这篇关于使用std :: enable_if作为templ时的默认模板参数。 param:为什么只有在enable_if参数不同的两个模板函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆