如何要求类型可以作为函数的参数

问题描述 投票:0回答:0

我只需要一个可以作为 std::wfstream::operator 参数的类型名 T<<

#include<fstream>
#include<string>

template<typename T> requires //Something here. I tested "std::invoke<std::wfstream::operator<<, K>"
void foo(T t){
    std::wfstream file{};
    file<<k;
};

void main(){
    std::string str;
    std::wstring wstr;

    std::wfstream file{};

    file<<str; //editor warns no operator << matches
    file<<wstr;
    file<<"s";
    file<<L"s";

    foo(str); //error while compile-time
    foo(wstr);
    foo("s");
    foo(L"s");
}

我查找了 std::invocable 和 std::invoke,但它是针对具有 operator() 的对象。

templates c++-concepts
© www.soinside.com 2019 - 2024. All rights reserved.