ben*_*ben 5 c++ operator-overloading
根据标准,转换函数有一个function-id operator conversion-type-id,就像operator char(&)[4]我相信的那样.但我无法弄清楚函数参数列表的放置位置.GCC不接受任何一种operator char(&())[4]或operator char(&)[4]()或任何我能想到的.
现在,gcc似乎接受(&operator char ())[4]但是clang不接受,而且我也倾向于不接受,因为它似乎不符合我所理解的语法.
我不想使用a,typedef因为我想避免使用它来污染命名空间.
你可以使用身份
template<typename T>
struct identity { typedef T type; };
struct sample {
operator identity<char[4]>::type &() {
...
}
};
Run Code Online (Sandbox Code Playgroud)
你是正确的,函数和数组声明符在转换函数中不起作用.这也是本期报告中已知和讨论的内容.但是我认为C++ 0x已经为他们在那里讨论的内容提供了解决方案
struct sample {
template<typename T>
using id = T;
template<typename T, int N>
operator id<T[N]> &() {
...
}
};
Run Code Online (Sandbox Code Playgroud)
不像identity和typedef方法,这允许T并N进行推断,我认为.
| 归档时间: |
|
| 查看次数: |
191 次 |
| 最近记录: |