Error while compiling code having | operator overloaded under template definition ,with VS2017 Update8.2












0














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) {
}
}









share|improve this question





























    0














    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) {
    }
    }









    share|improve this question



























      0












      0








      0







      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) {
      }
      }









      share|improve this question















      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++






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 7:43

























      asked Oct 29 at 8:38









      rish626509

      154




      154
























          1 Answer
          1






          active

          oldest

          votes


















          0














          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






          share|improve this answer





















          • Thanks for the answer
            – rish626509
            Oct 29 at 11:46











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          0














          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






          share|improve this answer





















          • Thanks for the answer
            – rish626509
            Oct 29 at 11:46
















          0














          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






          share|improve this answer





















          • Thanks for the answer
            – rish626509
            Oct 29 at 11:46














          0












          0








          0






          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






          share|improve this answer












          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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 29 at 9:23









          Kingsley Chen

          14819




          14819












          • 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




          Thanks for the answer
          – rish626509
          Oct 29 at 11:46


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Florida Star v. B. J. F.

          Error while running script in elastic search , gateway timeout

          Adding quotations to stringified JSON object values