【发布时间】:2020-02-03 13:49:11
【问题描述】:
在下面的代码中,A 是一个模板类,取决于一个非类型的bool type 参数。为A<true> 和A<false> 定义了一个朋友operator<<。 operator<< 还依赖于另一个 bool 模板参数。
#include <ostream>
#include <iostream>
#include <type_traits>
template <bool type>
class A;
template <>
class A<true> {
int m_x;
public:
A(int x) : m_x{x} { }
template <bool d, bool type>
friend std::ostream& operator<<(std::ostream& os, const A<type>& a);
};
template <>
class A<false> {
int m_y;
public:
A(int y) : m_y{y} { }
template <bool d, bool type>
friend std::ostream& operator<<(std::ostream& os, const A<type>& a);
};
template <bool d, bool type>
std::ostream& operator<<(std::ostream& os, const A<type>& a)
{
if constexpr (type) {
os << "m_x = " << a.m_x << std::endl;
if constexpr (d) { os << "2m_x = " << a.m_x << std::endl; }
}
else {
os << "m_y = " << a.m_y << std::endl;
if constexpr (d) { os << "2m_y = " << a.m_y << std::endl; }
}
return os;
}
int main()
{
A<true> atrue{2};
A<false> afalse{3};
operator<< <true>(std::cout, atrue);
operator<< <false>(std::cout, atrue);
operator<< <true>(std::cout, afalse);
operator<< <false>(std::cout, afalse);
return 0;
}
现在,我想给operator<< 的模板参数d 的默认值,比如d=false 这样的语句
std::cout << atrue;
等价于
operator<< <false>(std::cout, atrue);
因为bool d 采用默认值d=false 而bool type 是从operator<< 的第二个参数推导出来的。
有允许这样做的语法吗?
如果我在朋友声明中插入默认参数
template <bool d = false, bool type>
friend std::ostream& operator<<(std::ostream& os, const A<type>& a);
我得到一个编译错误:
main.cpp:14:71: 错误:默认模板参数不能用于 模板友元声明
如果我在operator<<的代码中插入默认参数
template <bool d = false, bool type>
std::ostream& operator<<(std::ostream& os, const A<type>& a)
{
...
再次编译时出现错误
main.cpp:27:15: 错误:朋友'template std::ostream& operator
27 | std::ostream& 运算符
main.cpp:14:26: 注意:'模板 std::ostream& operator
14 |朋友 std::ostream& 运算符
【问题讨论】:
-
不如使用
operator <<以外的名称作为print,因为您不能“正常”使用它?
标签: c++ templates operator-overloading friend