简体   繁体   English

编译具有|的代码时出错 VS2017 Update8.2在模板定义下重载了运算符

[英]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.

相关问题 如何以编程方式获取VS2017 Update8.2中/ permissve-设置的所有编译器标志 - How to get programatically all the compiler flags set by /permissve- in VS2017 Update8.2 VS2017 #error:: snprintf 的宏定义与标准库函数声明冲突 - VS2017 #error: : Macro definition of snprintf conflicts with Standard Library function declaration VS2017上SFINAE的编译错误 - compile error for SFINAE on VS2017 链接器错误构建GDAL VS2017 - Linker Error Building GDAL VS2017 VS2017 C ++ ifstream错误 - VS2017 c++ ifstream error 对重载的运算符使用模板时出错= - Error using template for overloaded operator= C ++:为什么这个constexpr模板函数在VS2017中导致内部错误?(在gcc中可以) - C++: Why does this constexpr template function cause an internal error in VS2017?(In gcc is ok) 模板类中的lambda函数的__declspec(noinline)会在VS2017中引发语法错误 - __declspec(noinline) on lambda function inside template class throws syntax error in VS2017 VS2017模板特化错误无法从&#39;Class *(__ cdecl *)(Args ...)&#39;转换为&#39;Class *(__ cdecl *)(Args ...)&#39; - VS2017 template specialization error cannot convert from 'Class *(__cdecl *)(Args…)' to 'Class *(__cdecl *)(Args…)' 在VS2017下使用Conan和CMake项目进行依赖管理 - Dependency management using Conan with a CMake project under VS2017
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM