小编mat*_*oro的帖子

如何模拟 C++17 之前的类模板参数推导?

我正在努力sscanf()从 C++ 代码库中删除调用,并用std::stringstream此处描述的实现替换它们:https : //www.quora.com/Is-there-aC-alternative-to-sscanf。相关代码是:

template<class Char> 
class imatch 
{ 
    const Char* s; 
public: 
    imatch(const Char* x) :s(x) {} 
     
    template<class Stream> 
    friend Stream& operator >> (Stream& st, const imatch& m) 
    { 
        std::basic_string<Char> x; 
        st >> x; //strip spaces, read chars up to space 
        if(x!=m.s) st.setstate(st.failbit); //set as "failure" a mismatch 
        return st; 
    } 
}; 
Run Code Online (Sandbox Code Playgroud)

然后在我的代码库中:

std::stringstream ss("value = 15"); //the input 
 
int val=0; 
ss >> imatch("value") >> imatch("=") >> val; 
 
if(ss) 
{ std::cout << …
Run Code Online (Sandbox Code Playgroud)

c++ gcc rhel c++17

2
推荐指数
1
解决办法
60
查看次数

标签 统计

c++ ×1

c++17 ×1

gcc ×1

rhel ×1