传递带有括号的 typedef 作为参数
Passing typedef with parenthesis as an argument
我找到了一段带有奇怪参数的代码,这是一种类型:
#include <iostream>
using namespace std;
template<class T>
int function1(T count,double)
{
cout<<"function1 is called"<<endl;
return 1111;
}
int main()
{
typedef int aaaa;
function1(1,aaaa());
}
而这个函数的输出是
我函数 1 被调用
想知道当参数是一种类型时是什么意思?为什么我应该给函数 aaaa() 和不带括号的 aaaa 给出编译器错误?
error: expected primary-expression before ‘)’ token
function1(1,aaaa);
^
function1(1, aaaa)
等效于function1(1, int)
- 您正在尝试将类型作为参数传递,该参数格式不正确。
在 function1(1,aaaa())
aaaa()
中创建具有值0
的临时int
。例如,请参阅C++14草案N4140 [expr.type.conv]/2:
表达式
;T()
,其中T
是非数组完整对象类型或(可能符合 cv 条件的)void 类型的简单类型说明符或类型名说明符,创建指定类型的 prvalue,其值是通过值初始化 (8.5) 生成的T
类型的对象
T();
其中T
是任何类型的,则会创建类型为 T
的无名临时。创建的对象将被值初始化。当应用于aaaa
(int
的typedef
)时,将创建一个临时int
并分配值0
。
相关文章:
- 如何将成员函数作为回调参数传递给需要"typedef-ed"自由函数指针的函数?
- typedef名称与可变模板参数名称一致时出现GCC错误
- 如何对绑定的成员方法进行typedef,然后将该类型用作模板参数
- 有没有一种方法可以使用typedef中的参数名称
- C++:显式调用模板参数的 typedef 的析构函数
- 如何在 C++ 中将 typedef 与类初始值设定项参数一起使用?
- 指向具有 2 个参数的 WINAPI 函数的 Typedef 指针
- 如何使用函数签名的 typedef 作为 std::function 的类型参数
- 如何在 C/C++ 中将 typedef 函数作为参数传递
- 如何从对象内部的 typedef 获取模板参数类型
- C 迭代器从Typedef std :: Map作为模板参数声明
- 将"typedef char IPString[17]"作为参数传递给函数
- Swig 简单类型 typedef 参数
- 如何将方法指针声明为Typedef方法参数
- c++检查模板参数的嵌套typedef以获取其标量基类型
- 以模板类型为参数的模板的typedef
- 如何用模板参数对函数指针进行typedef
- 使用模板参数的typedef不适用于g++
- 推导 typedef'd 函数指针的模板参数
- C++:使用"typedef"定义的类作为参数进行传递