模板参数替换失败,并且未完成隐式转换
Template argument substitution fails and implicit conversion is not done
#include <type_traits>
template<bool Const>
struct view_tpl {
using value_type = std::conditional_t<Const, const int, int>;
value_type* ptr;
view_tpl() = default;
view_tpl(const view_tpl<false>& other) : ptr(other.ptr) { }
};
using view = view_tpl<false>;
using const_view = view_tpl<true>;
void read(const const_view& vw) { }
int main() {
view vw;
read(vw);
}
此代码将常量和非常量视图类型定义为view_tpl<Const>
模板的别名。应该是view
可以隐式转换为const_view
,但不能反过来。
如果Const
是true
,则定义的复制构造函数将启用此功能,编译器将生成一个额外的默认复制构造函数。如果Const
是false
,则定义的复制构造函数将替换默认的复制构造函数。
这种隐式转换应该在调用f(vw)
时发生。
它在上面的代码中正常工作。
但如果我向模板(int N
(添加一个参数,并将f
和两个类型别名转换为模板,它将不再工作:
#include <type_traits>
template<int N, bool Const>
struct view_tpl {
using value_type = std::conditional_t<Const, const int, int>;
value_type* ptr;
view_tpl() = default;
view_tpl(const view_tpl<N, false>& other) : ptr(other.ptr) { }
};
template<int N> using view = view_tpl<N, false>;
template<int N> using const_view = view_tpl<N, true>;
template<int N>
void read(const const_view<N>& vw) { }
int main() {
view<0> vw;
read(vw);
}
编译器没有进行view_tpl<0, true>
到view_tpl<0, false>
的转换,而是只尝试直接的模板替换,但失败了:
main.cpp: In function 'int main()':
main.cpp:20:12: error: no matching function for call to 'read(view<0>&)'
20 | read(vw);
| ^
main.cpp:16:6: note: candidate: 'template<int N> void read(const_view<N>&)'
16 | void read(const const_view<N>& vw) { }
| ^~~~
main.cpp:16:6: note: template argument deduction/substitution failed:
main.cpp:20:12: note: template argument 'false' does not match 'true'
20 | read(vw);
| ^
有没有一种方法可以在不更改太多代码的情况下实现这一点?(实际代码比这个例子更复杂(
不幸的是,在模板参数推导中不会考虑隐式转换。
类型推导不考虑隐式转换(类型除外上面列出的调整(:这就是解决过载的工作,这将在以后发生。
您可以显式指定模板参数以通过传递推导,然后隐式转换稍后会正常工作。例如
view<0> vw;
read<0>(vw);
或者应用显式转换并将其包装到辅助对象中。
template<int N>
void read(const view<N>& vw) { read(static_cast<const_view<N>>(vw)); }
我们可以通过重载这两种类型来显式强制转换并传递给read( const const_view<N> )
函数。
#include <type_traits>
template<int N, bool Const>
struct view_tpl {
using value_type = std::conditional_t<Const, const int, int>;
value_type* ptr;
view_tpl() = default;
view_tpl(const view_tpl<N, false>& other) : ptr(other.ptr) { }
};
template<int N> using view = view_tpl<N, false>;
template<int N> using const_view = view_tpl<N, true>;
template<int N>
void read( const const_view<N>& vw )
{
//
}
template<int N>
void read( const view<N>& vw )
{
const_view<N> vw_converted { vw };
read( vw_converted );
}
int main() {
view<0> vw;
const_view<1> cvw;
read(vw);
read(cvw);
}
在线运行
或者,如果您不介意const_view
的额外副本
#include <type_traits>
template<int N, bool Const>
struct view_tpl {
using value_type = std::conditional_t<Const, const int, int>;
value_type* ptr;
view_tpl() = default;
view_tpl(const view_tpl<N, false>& other) : ptr(other.ptr) { }
};
template<int N> using view = view_tpl<N, false>;
template<int N> using const_view = view_tpl<N, true>;
template<int N, bool Const>
void read(const view_tpl<N,Const>& vw ) {
view_tpl<N,true> inst { vw }; // Now it is const
// But one extra copy for const_view
}
int main() {
view<0> vw;
const_view<1> cvw;
read(vw);
read(cvw);
}
在线运行
相关文章:
- 模板参数替换失败,并且未完成隐式转换
- 链接器输入文件未使用,因为链接未完成,我无法获得.o
- 我对C 中共享指针列表进行排序的功能未完成类型
- 我的循环未完成,当文件到达文件末尾时如何关闭它
- ADL 失败(或未完成?)具有附加(非推导)模板参数的函数
- 我的 c++ 程序未完成并关闭
- 程序未完成返回值3221225477没有编译器错误
- 多嵌套的循环C++未完成
- closesocket()未完成IOCP的挂起操作
- 未完成的纯虚拟方法
- 结构未完成排序
- 在所有线程未完成打印时提升线程
- Boost Asio未完成对套接字的写入
- 阿托伊未正确转换字符串
- gRPC c++ 多个未完成的请求
- C++ COUT 未强制转换内存 (void)
- 如何为未完成的枚举到字符串的转换生成编译错误
- 主线程关闭,而工作线程未完成
- Makefile中未完成链接
- 将字符串转换为char,空格字符未正确转换