为什么在"typename Operation::second_argument_type(x)"中使用typename?
Why does it use typename in "typename Operation::second_argument_type(x)"?
这是绑定的第二个定义:
template <class Operation, class T>
binder2nd<Operation> bind2nd (const Operation& op, const T& x)
{
return binder2nd<Operation>(op, typename Operation::second_argument_type(x));
}
关键字类型名称可用于:
在模板声明中,类型名可以用作用于声明类型模板参数的类。
在模板的声明或定义中,类型名可以是用于声明依赖名称是一个类型。
所以,我认为typename用于声明Operation::second_argument_type是一种类型,但我想知道为什么我们需要在这里使用typename?我们不能使用它吗?使用它有什么好处?
C++语法取决于标识符是否是一种类型。如果没有该知识,则无法解析语句。
通常,编译器只查看标识符是否已声明为类型。但是,如果符号依赖于模板参数,则编译器无法查看。所以你必须告诉它。
如果您使用
Operation::second_argument_type
它是一种类型,编译将失败,因为假设它是一个对象并且如果您使用
typename Operation::second_argument_type
它不是一个类型,编译将失败,因为假设它是一个类型,模板已经被解析了。
相关文章:
- 表示"accepting anything for this template argument" C++概念的通配符
- 为什么返回类型中需要typename?C++
- C++ assigment std::list:<typename>:itrator 在 main 中工作,但在方法中它不起作用
- 使用 'typename' 关键字将非类型视为依赖上下文中的类型
- C++模板错误:"invalid explicitly-specified argument for template parameter"
- 在"template"和函数声明之间使用:template<typename trait> using tr = base_trait<trait> void fn(tr::t
- 为什么编译器说"candidate template ignored: couldn't infer template argument 'InputIterator'"?
- 在这种情况下,"typename..."意味着什么?
- 为什么不需要在 C++20 中的依赖类型之前指定"typename"?
- "Cannot convert Python object argument to type '<typename>'" - 使用 Cython 包装 C++ 类时出错
- "typename"不应该只在模板函数或模板类中使用吗?
- 实现多类型算术运算符时如何解决"template argument deduction/substitution failure"
- C++线程"Call to non-static member function without an object argument"
- C++ 中的"template <typename From, typename Tag> struct Alias;"是什么?
- 在C++中,我想通过使用来自变量(例如字符串)的typename信息,从模板中定义一个类对象
- 尝试在类中编译内核,出现错误"__init__() got an unexpected keyword argument 'kernel'"
- 如何将一个使用参数包和typename的类作为函数(c++)的输入参数
- SFINAE的Typename关键字
- 为什么在以下情况下不需要为依赖类型使用typename
- 有什么技巧可以避免在模板类中使用"typename"关键字吗