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:
sstream(270): error C2131: expression did not evaluate to a constant
sstream(270): note: failure was caused by call of undefined function or one not declared 'constexpr'
sstream(270): note: see usage of '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(730): note: see reference to class template instantiation'std::basic_stringbuf,std::allocator>' being compiled
test.cpp(3): note: see reference to class template instantiation 'std::basic_stringstream,std::allocator>' being compiled
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
#include "test.hpp"
#include <sstream>
namespace testing
{
struct Message {
std::stringstream ss_;
};
}
using namespace ABC;
int main() {
return 0;
}
header file test.hpp
namespace ABC
{
template <typename T>
bool operator|(T v1, T v2) {
}
}
c++
add a comment |
My question is that while compiling C++ source code with Visual Studio 2017 Update 8.2, I am facing compilation error with message saying:
sstream(270): error C2131: expression did not evaluate to a constant
sstream(270): note: failure was caused by call of undefined function or one not declared 'constexpr'
sstream(270): note: see usage of '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(730): note: see reference to class template instantiation'std::basic_stringbuf,std::allocator>' being compiled
test.cpp(3): note: see reference to class template instantiation 'std::basic_stringstream,std::allocator>' being compiled
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
#include "test.hpp"
#include <sstream>
namespace testing
{
struct Message {
std::stringstream ss_;
};
}
using namespace ABC;
int main() {
return 0;
}
header file test.hpp
namespace ABC
{
template <typename T>
bool operator|(T v1, T v2) {
}
}
c++
add a comment |
My question is that while compiling C++ source code with Visual Studio 2017 Update 8.2, I am facing compilation error with message saying:
sstream(270): error C2131: expression did not evaluate to a constant
sstream(270): note: failure was caused by call of undefined function or one not declared 'constexpr'
sstream(270): note: see usage of '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(730): note: see reference to class template instantiation'std::basic_stringbuf,std::allocator>' being compiled
test.cpp(3): note: see reference to class template instantiation 'std::basic_stringstream,std::allocator>' being compiled
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
#include "test.hpp"
#include <sstream>
namespace testing
{
struct Message {
std::stringstream ss_;
};
}
using namespace ABC;
int main() {
return 0;
}
header file test.hpp
namespace ABC
{
template <typename T>
bool operator|(T v1, T v2) {
}
}
c++
My question is that while compiling C++ source code with Visual Studio 2017 Update 8.2, I am facing compilation error with message saying:
sstream(270): error C2131: expression did not evaluate to a constant
sstream(270): note: failure was caused by call of undefined function or one not declared 'constexpr'
sstream(270): note: see usage of '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(730): note: see reference to class template instantiation'std::basic_stringbuf,std::allocator>' being compiled
test.cpp(3): note: see reference to class template instantiation 'std::basic_stringstream,std::allocator>' being compiled
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
#include "test.hpp"
#include <sstream>
namespace testing
{
struct Message {
std::stringstream ss_;
};
}
using namespace ABC;
int main() {
return 0;
}
header file test.hpp
namespace ABC
{
template <typename T>
bool operator|(T v1, T v2) {
}
}
c++
c++
edited Nov 12 at 7:43
asked Oct 29 at 8:38
rish626509
154
154
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
constexpr auto _Both = ios_base::in | ios_base::out;
with your operator |
, and (un)fortunately, your overload function is not a constexpr-function.
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
Thanks for the answer
– rish626509
Oct 29 at 11:46
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53041609%2ferror-while-compiling-code-having-operator-overloaded-under-template-definitio%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
constexpr auto _Both = ios_base::in | ios_base::out;
with your operator |
, and (un)fortunately, your overload function is not a constexpr-function.
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
Thanks for the answer
– rish626509
Oct 29 at 11:46
add a comment |
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
constexpr auto _Both = ios_base::in | ios_base::out;
with your operator |
, and (un)fortunately, your overload function is not a constexpr-function.
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
Thanks for the answer
– rish626509
Oct 29 at 11:46
add a comment |
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
constexpr auto _Both = ios_base::in | ios_base::out;
with your operator |
, and (un)fortunately, your overload function is not a constexpr-function.
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
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
constexpr auto _Both = ios_base::in | ios_base::out;
with your operator |
, and (un)fortunately, your overload function is not a constexpr-function.
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
answered Oct 29 at 9:23
Kingsley Chen
14819
14819
Thanks for the answer
– rish626509
Oct 29 at 11:46
add a comment |
Thanks for the answer
– rish626509
Oct 29 at 11:46
Thanks for the answer
– rish626509
Oct 29 at 11:46
Thanks for the answer
– rish626509
Oct 29 at 11:46
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53041609%2ferror-while-compiling-code-having-operator-overloaded-under-template-definitio%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown