Lor*_*one 17 c++ templates sfinae c++11
有人可以解释为什么编译器只接受这个代码
template<typename L, size_t offset, typename enable_if< (offset<sizeof(L)), int >::type =0>
void a_function(){}
template<typename L, size_t offset, typename enable_if< (offset==sizeof(L)), int >::type =0>
void a_function(){}
Run Code Online (Sandbox Code Playgroud)
但不是这个:
template<typename L, size_t offset, typename enable_if< (offset<sizeof(L)), int >::type =0>
class a_class{};
template<typename L, size_t offset, typename enable_if< (offset==sizeof(L)), int >::type =0>
class a_class{};
Run Code Online (Sandbox Code Playgroud)
编译器将第二个类模板视为第一个类的重新定义.
jpa*_*cek 15
你必须使用专门化的类.通常,它使用额外的参数完成:
template <class P, class dummy = void>
class T;
template <class P>
class T<P, typename enable_if<something, void>::type> {
the real thing
};
Run Code Online (Sandbox Code Playgroud)
具有相同名称的两个类(或类模板)声明应始终声明相同的类或类模板(或者是特化,在这种情况下它仍然是相同的模板).
| 归档时间: |
|
| 查看次数: |
3028 次 |
| 最近记录: |