考虑以下代码:
template <typename T>
struct X
{
void foo(){}
};
class Y { };
template <typename T, typename... U>
class Example {
protected:
template <template<typename> typename example_t>
using alias = X<example_t<T>>;
};
template <typename T>
struct ExampleA : public Example<T, Y>
{
using MyAlias = ExampleA::alias<ExampleA>;
};
Run Code Online (Sandbox Code Playgroud)
在C ++ 14中,我可以执行以下操作并使它按预期工作:
ExampleA<int>::MyAlias test;
test.foo();
Run Code Online (Sandbox Code Playgroud)
现在最近升级到C ++ 17会发出警告'ExampleA<T>::alias': dependent name is not a type以及syntax error: identifier 'alias'。
通常,当您遇到涉及a的事物时,dependent name这意味着您需要添加typename关键字,如以下示例所示(iterator取决于std::vector<T>):
template<typename …Run Code Online (Sandbox Code Playgroud) std::cout我想扩展使用我自己的控制台/cout 包装类的用法。
理想情况下,我会有 2 个 ostream,一个用于常规打印,另一个用于附加新行。
std::ostream Write;
Write << "Hello, I am " << 99 << " years old.";
Run Code Online (Sandbox Code Playgroud)
印刷Hello, I am 99 years old.
std::ostream WriteLine;
WriteLine << "Hello, I am " << 99 << " years old.";
Run Code Online (Sandbox Code Playgroud)
打印Hello, I am 99 years old.\n(一个实际的新行,而不仅仅是转义)
然后,我想扩展它以具有错误流(例如Error和),该错误流在消息之前添加前缀并以不同的颜色打印。ErrorLine"ERROR: "
我知道我必须创建自己的流来添加此功能,并且我遵循C++ cout 并使用前缀作为前缀std::cout,这几乎是我想要的,但不完全是。我不知道如何在流的末尾添加新行,并且前缀不可靠,特别是当我要执行多个打印语句时。
我还应该提到我不想使用重载运算符来实现这种效果,因为我希望能够以菊花链方式连接事物。
什么不起作用
如果我WriteLine >> "First";这样做,WriteLine << "Second";我会得到奇怪的结果,例如SecondFirst\n或Second\nFirst。我理想的输出是First\nSecond\n. …