gpt4 book ai didi

c++ - 为什么模板运算符<< 不推导出 std::endl?

转载 作者:行者123 更新时间:2023-11-28 01:23:32 25 4
gpt4 key购买 nike

<分区>

如果您取消注释第一个运算符定义,这将编译并运行:

#include <iostream>

struct logger
{
std::ostream &loggingStream;
logger(std::ostream &ls) : loggingStream(ls) {}

};

/*
logger &operator<<(logger &l, std::ostream & (*manip)(std::ostream &)) {
manip(l.loggingStream);
return l;
}
*/

template<typename T>
logger &operator<<(logger &l, const T &t) {
l.loggingStream << t;
return l;
}

int main() {
logger l(std::cout);

l << "Hello" << std::endl;
return 0;
}

有了评论:

error: no match for ‘operator<<’ (operand types are ‘logger’ and ‘<unresolved overloaded function type>’)

为什么我需要提供一个非模板重载来处理 endl

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com