![](/img/trans.png)
[英]How to get programatically all the compiler flags set by /permissve- in VS2017 Update8.2
[英]Error while compiling code having | operator overloaded under template definition ,with VS2017 Update8.2
我的問題是,在使用Visual Studio 2017 Update 8.2編譯C ++源代碼時,我遇到編譯錯誤並顯示以下消息:
sstream(270):錯誤C2131:表達式未求值為常數
sstream(270):注意:失敗是由於調用未定義函數或未聲明“ constexpr”引起的
sstream(270):注意:請參閱“ operator |”的用法
sstream(249):注意:編譯類模板成員函數'std :: fpos <_Mbstatet> std :: basic_stringbuf,std :: allocator> :: seekoff(__ int64,std :: ios_base :: seekdir,std :: ios_base: :用於openmode)”
sstream(730):注意:請參見對正在編譯的類模板實例化'std :: basic_stringbuf,std :: allocator>'的引用
test.cpp(3):注意:請參見對正在編譯的類模板實例化'std :: basic_stringstream,std :: allocator>'的引用
我正在共享可能產生相同編譯錯誤的代碼段。 請有人幫助我。
給出的答案很好,解決了幾個問題。 但是我面臨另一個問題。 我將重載的模板放在命名空間中,但將其放在頭文件中,因為現有代碼在頭文件中具有重載的模板。 我再次面臨着同樣的問題。 我現在正在更新代碼
我的代碼:
cpp文件test.cpp
#include "test.hpp"
#include <sstream>
namespace testing
{
struct Message {
std::stringstream ss_;
};
}
using namespace ABC;
int main() {
return 0;
}
頭文件test.hpp
namespace ABC
{
template <typename T>
bool operator|(T v1, T v2) {
}
}
為重載運算符定義全局函數模板確實很危險,因為它將影響與使用已重載運算符的作用域相同范圍內的現有代碼。
您的示例中的錯誤是因為MSVC嘗試編譯
constexpr auto _Both = ios_base::in | ios_base::out;
與您的operator |
和(不幸的),您的重載函數不是constexpr函數。
解決方案很簡單:將重載的模板放入命名空間中:
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;
}
}
撇開:也許您可以通過以下網址查看STL如何做到這一點: https : //en.cppreference.com/w/cpp/utility/rel_ops/operator_cmp
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.