【发布时间】:2014-09-10 05:52:04
【问题描述】:
这是一个返回数组引用的转换函数:
struct S {
typedef int int_array_20[20];
operator int_array_20& ();
};
没有typedef 也可以做同样的事情吗?我试过的:
struct S {
operator int (&()) [10];
};
但是clang抱怨:
error: C++ requires a type specifier for all declarations
operator int (&()) [10];
~ ^
error: conversion function cannot have any parameters
operator int (&()) [10];
^
error: must use a typedef to declare a conversion to 'int [10]'
error: conversion function cannot convert to an array type
做:
必须使用 typedef 来声明转换为 'int [10]'
意思是typedef是必不可少的吗?
编辑
如果typedef是必要的,就不可能创建如下的转换函数模板,因为无法定义typedef模板,对吧?
struct S {
template<typename T, int N>
operator T(&())[N];
};
【问题讨论】:
-
是的,你必须使用
typedef,因为它清楚地告诉你。 -
我现在真的不想跟随这条链,但从这里开始:i.imgur.com/1eqfyiW.png
标签: c++ type-conversion typedef language-lawyer conversion-operator