c++ - 错误 : cannot add a default template argument

当我使用 c++11 编译以下程序时,出现错误 error: cannot add a default template argument to the definition of a member of a class template。为什么编译器不允许您在成员函数定义中使用默认模板参数?

#include <iostream>

template<typename A, typename T = int>
class my_class
{
private:
  void dosomething();
};

template<typename A, typename T = int>
void my_class<A, T>::dosomething()
{

}

int main( ) 
{ 
  B<int> obj;

  return 0;
}

最佳答案

来自 [temp.param]/9 (粗体是我强调)

A default template-argument is a template-argument (17.3) specified after = in a template-parameter. A default template-argument may be specified for any kind of template-parameter (type, non-type, template) that is not a template parameter pack (17.5.3). A default template-argument may be specified in a template declaration. A default template-argument shall not be specified in the template-parameter-lists of the definition of a member of a class template that appears outside of the member’s class. A default template-argument shall not be specified in a friend class template declaration. If a friend function template declaration specifies a default template-argument, that declaration shall be a definition and shall be the only declaration of the function template in the translation unit.

关于c++ - 错误 : cannot add a default template argument to the definition of a member of a class template,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60098826/

相关文章:

kubernetes-helm - 将 yaml 转换为 helm 模板中的属性文件

python - 如何计算具有特定扩展名或名称的文件中的文件数?

reactjs - react 导航 v5 中的 tabBarIcon 问题

php - 如何从时间戳对象和日期时间对象数组中提取值

reactjs - Ant 设计排序表代码不适用于 react typescript

javascript - Flask Jinja2 模板语法与 AngularJS 冲突(jinja

python - Pyspark - 将多列数据组合成一个跨行分布的列

r - 提取第n个字符直到R中的字符串结尾

php - 是否有一个 Wordpress php 函数可以只返回自定义 Logo 图像的 URL(

spring - 带有 Spring Cloud Gateway 的 Keycloak