[英]Error while compiling code having | operator overloaded under template definition ,with VS2017 Update8.2
My question is that while compiling C++ source code with Visual Studio 2017 Update 8.2, I am facing compilation error with message saying: 我的问题是,在使用Visual Studio 2017 Update 8.2编译C ++源代码时,我遇到编译错误并显示以下消息:
sstream(270): error C2131: expression did not evaluate to a constant sstream(270):错误C2131:表达式未求值为常数
sstream(270): note: failure was caused by call of undefined function or one not declared 'constexpr' sstream(270):注意:失败是由于调用未定义函数或未声明“ constexpr”引起的
sstream(270): note: see usage of 'operator |' sstream(270):注意:请参阅“ operator |”的用法
sstream(249): note: while compiling class template member function 'std::fpos<_Mbstatet> std::basic_stringbuf,std::allocator>::seekoff(__int64,std::ios_base::seekdir,std::ios_base::openmode)' sstream(249):注意:编译类模板成员函数'std :: fpos <_Mbstatet> std :: basic_stringbuf,std :: allocator> :: seekoff(__ int64,std :: ios_base :: seekdir,std :: ios_base: :用于openmode)”
sstream(730): note: see reference to class template instantiation'std::basic_stringbuf,std::allocator>' being compiled sstream(730):注意:请参见对正在编译的类模板实例化'std :: basic_stringbuf,std :: allocator>'的引用
test.cpp(3): note: see reference to class template instantiation 'std::basic_stringstream,std::allocator>' being compiled test.cpp(3):注意:请参见对正在编译的类模板实例化'std :: basic_stringstream,std :: allocator>'的引用
I am sharing the code snippet which can produce same compilation error. 我正在共享可能产生相同编译错误的代码段。 Please someone help me with this. 请有人帮助我。
The answer given was good and solved few issues . 给出的答案很好,解决了几个问题。 however I am facing another issue . 但是我面临另一个问题。 I put the overloaded template into a namespace but put that in header file , because the existing code has the overloaded template in a header file . 我将重载的模板放在命名空间中,但将其放在头文件中,因为现有代码在头文件中具有重载的模板。 Again I am facing same issue . 我再次面临着同样的问题。 I am updating the code now 我现在正在更新代码
My Code: 我的代码:
cpp file test.cpp cpp文件test.cpp
#include "test.hpp"
#include <sstream>
namespace testing
{
struct Message {
std::stringstream ss_;
};
}
using namespace ABC;
int main() {
return 0;
}
header file test.hpp 头文件test.hpp
namespace ABC
{
template <typename T>
bool operator|(T v1, T v2) {
}
}
Defining global function templates for overloading operators is truly dangerous, because it will affect existing code within the same scope that uses the operators your have overloaded. 为重载运算符定义全局函数模板确实很危险,因为它将影响与使用已重载运算符的作用域相同范围内的现有代码。
The error in your sample is because MSVC tries to compile 您的示例中的错误是因为MSVC尝试编译
constexpr auto _Both = ios_base::in | ios_base::out;
with your operator |
与您的operator |
, and (un)fortunately, your overload function is not a constexpr-function. 和(不幸的),您的重载函数不是constexpr函数。
The solution is simple: put your overloaded template into a namespace: 解决方案很简单:将重载的模板放入命名空间中:
namespace ext_ops {
// operator to combine two parameter attributes v1 and v2, e.g.,
template <typename T>
bool operator|(T v1, T v2) {
return false;
}
}
Aside: maybe you could check out how the STL had done that via: https://en.cppreference.com/w/cpp/utility/rel_ops/operator_cmp 撇开:也许您可以通过以下网址查看STL如何做到这一点: https : //en.cppreference.com/w/cpp/utility/rel_ops/operator_cmp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.