以下代码如下:
std::stringstream os;
os << std::hex; // MISRA warning on this line
os << std::setw(2);
os << std::setfill('0');
Run Code Online (Sandbox Code Playgroud)
警告:"必需规则8-4-4,没有'&'或parenthisized参数列表使用的函数标识符"
我无法解决此问题,请提出解决方案.
&像建议一样使用怎么样?
#include <iomanip>
#include <iostream>
#include <sstream>
int main() {
std::stringstream os;
os << &std::hex; // Works with &
os << std::setw(2);
os << std::setfill('0');
os << 13;
std::cout << os.str() << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
是的,它也有效。
有什么不同 ?
std::hex是对函数的引用&std::hex是一个指向函数的指针由于对函数的引用具有到函数指针的隐式转换,因此您可以将其传递给 anostream并且它会按预期工作。不过,显然 MISRA 要求您明确表示您的意思是我想要该函数还是我想要调用该函数。