C++ 如何将std::ifstream转换为std::basic\u istream<;图表、特征>&;?

C++ 如何将std::ifstream转换为std::basic\u istream<;图表、特征>&;?,c++,templates,std,ifstream,istream,C++,Templates,Std,Ifstream,Istream,我有以下方法: template<typename CharT, typename Traits, typename Alloc> auto getline_n(std::basic_istream<CharT, Traits>& in, std::basic_string<CharT, Traits, Alloc>& str, std::streamsize n) -> decltype(in) 模板 自动获取行(std::basic

我有以下方法:

template<typename CharT, typename Traits, typename Alloc>
auto getline_n(std::basic_istream<CharT, Traits>& in, std::basic_string<CharT, Traits, Alloc>& str, std::streamsize n) -> decltype(in)
模板
自动获取行(std::basic\u istream&in,std::basic\u string&str,std::streamsize n)->decltype(in)
此方法的完整版本包含在底部

我需要能够将std::ifstream转换为std::basic_istream&这样我就可以将它传递给这个方法

下面是调用代码的样子:

std::ifstream pipe2;
pipe2 = std::ifstream {};
pipe2.open("/<pathToNamedPipe>");
std::basic_istream<char,???> s = pipe2;
std::string line{};
getline_n(s, line, 50);
std::ifstream pipe2;
pipe2=std::ifstream{};
管道2.打开(“/”);
标准:基本流s=管道2;
std::字符串行{};
getline_n(s,line,50);
不太清楚性病的特征以及它们是什么:基本的?看见上面

示例2(因为人们问我为什么不通过pipe2,这是第一件尝试的事情),如:

std::ifstream pipe2;
pipe2=std::ifstream{};
管道2.打开(“/”);
std::字符串行{};
getline_n(管道2,管道50);
然后我得到一个Xcode编译器错误:

"'std::ifstream' (aka 'basic_ifstream<char>') to 'char **'."
“'std::ifstream'(也称为'basic_ifstream')到'char**.”
这就是我试图将其转换为std::basic_istream的原因

下面是我要调用的完整方法:

template<typename CharT, typename Traits, typename Alloc>
auto getline_n(std::basic_istream<CharT, Traits>& in, std::basic_string<CharT, Traits, Alloc>& str, std::streamsize n) -> decltype(in) {
    std::ios_base::iostate state = std::ios_base::goodbit;
    bool extracted = false;
    const typename std::basic_istream<CharT, Traits>::sentry s(in, true);
    if(s) {
        try {
            str.erase();
            typename Traits::int_type ch = in.rdbuf()->sgetc();
            for(; ; ch = in.rdbuf()->snextc()) {
                if(Traits::eq_int_type(ch, Traits::eof())) {
                    // eof spotted, quit
                    state |= std::ios_base::eofbit;
                    break;
                }
                else if(str.size() == n) {
                    // maximum number of characters met, quit
                    extracted = true;
                    in.rdbuf()->sbumpc();
                    break;
                }
                else if(str.max_size() <= str.size()) {
                    // string too big
                    state |= std::ios_base::failbit;
                    break;
                }
                else {
                    // character valid
                    str += Traits::to_char_type(ch);
                    extracted = true;
                }
            }
        }
        catch(...) {
            in.setstate(std::ios_base::badbit);
        }
    }

    if(!extracted) {
        state |= std::ios_base::failbit;
    }
    in.setstate(state);
    return in;
}
模板
自动获取行(std::basic\u istream&in,std::basic\u string&str,std::streamsize n)->decltype(in){
std::ios_base::iostate state=std::ios_base::goodbit;
bool=false;
const typename std::basic_istream::sentry s(in,true);
若有(s){
试一试{
str.erase();
typename Traits::int_type ch=in.rdbuf()->sgetc();
对于(;;ch=in.rdbuf()->snextc()){
if(Traits::eq_int_type(ch,Traits::eof())){
//eof,退出
状态|=std::ios_base::eofbit;
打破
}
else if(str.size()==n){
//满足的最大字符数,退出
提取=真;
in.rdbuf()->sbumpc();
打破
}

否则,如果(str.max_size()您没有。

您的
std::ifstream
已从
std::basic\u istream
派生

函数就是这样定义的,因此如果您愿意,您可以传递另一种流的[reference]


程序中其他地方没有错误,此代码已经运行。

我不确定问题出在哪里。您可以从
ifstream
读取,也可以从
istream
读取。是否
getline\n
正在执行特别需要
istream
的操作?@cigien如果我试图通过管道2,我会收到此错误:no从'std::ifstream'(也称为'basic_ifstream'的可行转换)到“char**”。我添加了一个包含此getline方法的SO帖子的链接。好的,但将该信息添加到问题中。另外,添加
getline\n
的实现;可能有一种更简单的方法来解决您的问题。@cigien添加了getline\n实现。好的,还可以精确显示您如何使用
pipe2
调用它,当你这样做的时候,你会得到什么错误信息。不,它不起作用。请看上面的例子2和我看到的Xcode LLVM Clang编译器错误。@bhartsb这些都与你给我们看的代码无关。你将一个
ifstream
传递给一个函数,该函数引用它的一个基类。就是它。句点。如果它对你不起作用,那么en你的错误不在问题的某个地方。也许另一个
getline\n
重载会占用
char**
?不管怎样,这个问题的答案都是不,你不需要将
ifstream
转换成那个东西;你不需要。你是对的,这是我的错误,编译单元中没有看到模板,所以我我收到的错误消息完全让我失望。我把它包括在调用它的地方。你的“可能是另一个获取字符**的getline重载?”给了我提示。我不知道为什么编译器会给我示例2的错误消息,而不是“使用未声明的标识符”。无论出于何种原因,它都不喜欢getline_n(pipe,line,50);例如,如果我完全注释掉模板方法,只留下调用它的那一行,我就会得到错误。如果删除第三个参数(50),我会得到另一个错误“Use of undeclared identifier”。BTW
pipe2=std::ifstream{};
完全没有意义。不使用这三行,只需像这样声明流:
std::ifstream pipe2(“”;
template<typename CharT, typename Traits, typename Alloc>
auto getline_n(std::basic_istream<CharT, Traits>& in, std::basic_string<CharT, Traits, Alloc>& str, std::streamsize n) -> decltype(in) {
    std::ios_base::iostate state = std::ios_base::goodbit;
    bool extracted = false;
    const typename std::basic_istream<CharT, Traits>::sentry s(in, true);
    if(s) {
        try {
            str.erase();
            typename Traits::int_type ch = in.rdbuf()->sgetc();
            for(; ; ch = in.rdbuf()->snextc()) {
                if(Traits::eq_int_type(ch, Traits::eof())) {
                    // eof spotted, quit
                    state |= std::ios_base::eofbit;
                    break;
                }
                else if(str.size() == n) {
                    // maximum number of characters met, quit
                    extracted = true;
                    in.rdbuf()->sbumpc();
                    break;
                }
                else if(str.max_size() <= str.size()) {
                    // string too big
                    state |= std::ios_base::failbit;
                    break;
                }
                else {
                    // character valid
                    str += Traits::to_char_type(ch);
                    extracted = true;
                }
            }
        }
        catch(...) {
            in.setstate(std::ios_base::badbit);
        }
    }

    if(!extracted) {
        state |= std::ios_base::failbit;
    }
    in.setstate(state);
    return in;
}