模板参数替换失败,并且未完成隐式转换

Template argument substitution fails and implicit conversion is not done

本文关键字:未完成 转换 参数 替换 失败      更新时间:2023-10-16
#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,但不能反过来。

如果Consttrue,则定义的复制构造函数将启用此功能,编译器将生成一个额外的默认复制构造函数。如果Constfalse,则定义的复制构造函数将替换默认的复制构造函数。

这种隐式转换应该在调用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);
}

在线运行