Template implementation for commutative operators, legal?Why can templates only be implemented in the header file?Where and why do I have to put the “template” and “typename” keywords?What is the “-->” operator in C++?What are the basic rules and idioms for operator overloading?Is a c++11 variadic function template overload with a dependent type ambiguous?Is it possible to use -1 to fetch the last element of a container/array?clarification of decltype output when multiplying 2 const intsImplementing is_constexpr_copiableClang can't find template binary operator in fold expressionError in template instantiation before overloading

Why is it that I can sometimes guess the next note?

Open a doc from terminal, but not by its name

Why Shazam when there is already Superman?

Creepy dinosaur pc game identification

Can disgust be a key component of horror?

Is this toilet slogan correct usage of the English language?

Can a Canadian Travel to the USA twice, less than 180 days each time?

Does IPv6 have similar concept of network mask?

Redundant comparison & "if" before assignment

How can I write humor as character trait?

Why does the Sun have different day lengths, but not the gas giants?

What if a revenant (monster) gains fire resistance?

Lowest total scrabble score

How to explain what's wrong with this application of the chain rule?

Can I visit Japan without a visa?

Why can Carol Danvers change her suit colours in the first place?

Is there a way to get `mathscr' with lower case letters in pdfLaTeX?

Does the Linux kernel need a file system to run?

Is aluminum electrical wire used on aircraft?

How could a planet have erratic days?

Can a College of Swords bard use a Blade Flourish option on an opportunity attack provoked by their own Dissonant Whispers spell?

Terse Method to Swap Lowest for Highest?

Can I still be respawned if I die by falling off the map?

Electoral considerations aside, what are potential benefits, for the US, of policy changes proposed by the tweet recognizing Golan annexation?



Template implementation for commutative operators, legal?


Why can templates only be implemented in the header file?Where and why do I have to put the “template” and “typename” keywords?What is the “-->” operator in C++?What are the basic rules and idioms for operator overloading?Is a c++11 variadic function template overload with a dependent type ambiguous?Is it possible to use -1 to fetch the last element of a container/array?clarification of decltype output when multiplying 2 const intsImplementing is_constexpr_copiableClang can't find template binary operator in fold expressionError in template instantiation before overloading













4















I've tried to implement a commutative addition operator for one of my classes:



struct mytype

constexpr mytype(othertype const &);
constexpr mytype operator+(othertype const &rhs) const;
;

template<typename T>
constexpr auto operator+(T const &lhs, mytype const &rhs) -> decltype(rhs + lhs)

return rhs + lhs;



The idea is that anything accepted on the right hand side also becomes acceptable on the left hand side as long as the right hand side is a mytype.



This works fine with icc and Visual Studio, and goes into an endless recursion resolving the decltype on gcc and clang (terminated when the maximum template depth is reached).



I can see that the endless recursion might in fact be more correct, as explained in the bug report: the specialization is needed before overload resolution takes place (because it is an input to overload resolution).



On the other hand the commercial compilers somehow manage (whether by accident or on purpose is probably debatable).



What is the correct behavior here?



Is it possible to avoid specifying a full list of classes for which operator+ is supposed to be commutative?



Compilable example:



struct othertype ;

struct mytype

constexpr mytype() : value(1)
constexpr mytype(int v) : value(v)
constexpr mytype(othertype const &o) : value(2) // 1

constexpr mytype operator+(mytype const &rhs) const

return mytype(value + rhs.value);

constexpr mytype operator+(othertype const &rhs) const // 2

return mytype(value + 2);


int value;
;

template<typename T>
constexpr auto operator+(T const &lhs, mytype const &rhs) -> decltype(rhs + lhs)

return rhs + lhs;


void test()

constexpr mytype mine;
constexpr othertype other;

constexpr auto result = other + mine;

static_assert(result.value == 3);



The problem goes away when the conversion // 1 is removed, which isn't helpful in my use case. The separate addition operator // 2 is not sufficient to help resolve the decltype: overload resolution should have picked that up, but the problem happens before overload resolution.



The infinite recursion happens after specializing the template for T = othertype: converting othertype to mytype gives an addition expression with mytype on both sides, which again can be resolved through the template (even though a non-template exists).










share|improve this question



















  • 1





    How do you use this operator? Could you perhaps show us a Minimal, Complete, and Verifiable example that exhibits the problem you have? Even if we can't replicate the problem, it could be useful to see how you use it. And it could be very useful to see your implementation of the member overload, and what othertype is.

    – Some programmer dude
    Mar 7 at 6:43












  • By the way, do you really need to non-explicit conversion operator? What happens if you remove it or make it explicit?

    – Some programmer dude
    Mar 7 at 6:56











  • @Someprogrammerdude, this is part of a refactoring — mytype is supposed to replace othertype in the long run, so it needs to be convertible to allow gradually replacing instances without breaking the rest of the program.

    – Simon Richter
    Mar 7 at 7:05















4















I've tried to implement a commutative addition operator for one of my classes:



struct mytype

constexpr mytype(othertype const &);
constexpr mytype operator+(othertype const &rhs) const;
;

template<typename T>
constexpr auto operator+(T const &lhs, mytype const &rhs) -> decltype(rhs + lhs)

return rhs + lhs;



The idea is that anything accepted on the right hand side also becomes acceptable on the left hand side as long as the right hand side is a mytype.



This works fine with icc and Visual Studio, and goes into an endless recursion resolving the decltype on gcc and clang (terminated when the maximum template depth is reached).



I can see that the endless recursion might in fact be more correct, as explained in the bug report: the specialization is needed before overload resolution takes place (because it is an input to overload resolution).



On the other hand the commercial compilers somehow manage (whether by accident or on purpose is probably debatable).



What is the correct behavior here?



Is it possible to avoid specifying a full list of classes for which operator+ is supposed to be commutative?



Compilable example:



struct othertype ;

struct mytype

constexpr mytype() : value(1)
constexpr mytype(int v) : value(v)
constexpr mytype(othertype const &o) : value(2) // 1

constexpr mytype operator+(mytype const &rhs) const

return mytype(value + rhs.value);

constexpr mytype operator+(othertype const &rhs) const // 2

return mytype(value + 2);


int value;
;

template<typename T>
constexpr auto operator+(T const &lhs, mytype const &rhs) -> decltype(rhs + lhs)

return rhs + lhs;


void test()

constexpr mytype mine;
constexpr othertype other;

constexpr auto result = other + mine;

static_assert(result.value == 3);



The problem goes away when the conversion // 1 is removed, which isn't helpful in my use case. The separate addition operator // 2 is not sufficient to help resolve the decltype: overload resolution should have picked that up, but the problem happens before overload resolution.



The infinite recursion happens after specializing the template for T = othertype: converting othertype to mytype gives an addition expression with mytype on both sides, which again can be resolved through the template (even though a non-template exists).










share|improve this question



















  • 1





    How do you use this operator? Could you perhaps show us a Minimal, Complete, and Verifiable example that exhibits the problem you have? Even if we can't replicate the problem, it could be useful to see how you use it. And it could be very useful to see your implementation of the member overload, and what othertype is.

    – Some programmer dude
    Mar 7 at 6:43












  • By the way, do you really need to non-explicit conversion operator? What happens if you remove it or make it explicit?

    – Some programmer dude
    Mar 7 at 6:56











  • @Someprogrammerdude, this is part of a refactoring — mytype is supposed to replace othertype in the long run, so it needs to be convertible to allow gradually replacing instances without breaking the rest of the program.

    – Simon Richter
    Mar 7 at 7:05













4












4








4








I've tried to implement a commutative addition operator for one of my classes:



struct mytype

constexpr mytype(othertype const &);
constexpr mytype operator+(othertype const &rhs) const;
;

template<typename T>
constexpr auto operator+(T const &lhs, mytype const &rhs) -> decltype(rhs + lhs)

return rhs + lhs;



The idea is that anything accepted on the right hand side also becomes acceptable on the left hand side as long as the right hand side is a mytype.



This works fine with icc and Visual Studio, and goes into an endless recursion resolving the decltype on gcc and clang (terminated when the maximum template depth is reached).



I can see that the endless recursion might in fact be more correct, as explained in the bug report: the specialization is needed before overload resolution takes place (because it is an input to overload resolution).



On the other hand the commercial compilers somehow manage (whether by accident or on purpose is probably debatable).



What is the correct behavior here?



Is it possible to avoid specifying a full list of classes for which operator+ is supposed to be commutative?



Compilable example:



struct othertype ;

struct mytype

constexpr mytype() : value(1)
constexpr mytype(int v) : value(v)
constexpr mytype(othertype const &o) : value(2) // 1

constexpr mytype operator+(mytype const &rhs) const

return mytype(value + rhs.value);

constexpr mytype operator+(othertype const &rhs) const // 2

return mytype(value + 2);


int value;
;

template<typename T>
constexpr auto operator+(T const &lhs, mytype const &rhs) -> decltype(rhs + lhs)

return rhs + lhs;


void test()

constexpr mytype mine;
constexpr othertype other;

constexpr auto result = other + mine;

static_assert(result.value == 3);



The problem goes away when the conversion // 1 is removed, which isn't helpful in my use case. The separate addition operator // 2 is not sufficient to help resolve the decltype: overload resolution should have picked that up, but the problem happens before overload resolution.



The infinite recursion happens after specializing the template for T = othertype: converting othertype to mytype gives an addition expression with mytype on both sides, which again can be resolved through the template (even though a non-template exists).










share|improve this question
















I've tried to implement a commutative addition operator for one of my classes:



struct mytype

constexpr mytype(othertype const &);
constexpr mytype operator+(othertype const &rhs) const;
;

template<typename T>
constexpr auto operator+(T const &lhs, mytype const &rhs) -> decltype(rhs + lhs)

return rhs + lhs;



The idea is that anything accepted on the right hand side also becomes acceptable on the left hand side as long as the right hand side is a mytype.



This works fine with icc and Visual Studio, and goes into an endless recursion resolving the decltype on gcc and clang (terminated when the maximum template depth is reached).



I can see that the endless recursion might in fact be more correct, as explained in the bug report: the specialization is needed before overload resolution takes place (because it is an input to overload resolution).



On the other hand the commercial compilers somehow manage (whether by accident or on purpose is probably debatable).



What is the correct behavior here?



Is it possible to avoid specifying a full list of classes for which operator+ is supposed to be commutative?



Compilable example:



struct othertype ;

struct mytype

constexpr mytype() : value(1)
constexpr mytype(int v) : value(v)
constexpr mytype(othertype const &o) : value(2) // 1

constexpr mytype operator+(mytype const &rhs) const

return mytype(value + rhs.value);

constexpr mytype operator+(othertype const &rhs) const // 2

return mytype(value + 2);


int value;
;

template<typename T>
constexpr auto operator+(T const &lhs, mytype const &rhs) -> decltype(rhs + lhs)

return rhs + lhs;


void test()

constexpr mytype mine;
constexpr othertype other;

constexpr auto result = other + mine;

static_assert(result.value == 3);



The problem goes away when the conversion // 1 is removed, which isn't helpful in my use case. The separate addition operator // 2 is not sufficient to help resolve the decltype: overload resolution should have picked that up, but the problem happens before overload resolution.



The infinite recursion happens after specializing the template for T = othertype: converting othertype to mytype gives an addition expression with mytype on both sides, which again can be resolved through the template (even though a non-template exists).







c++ c++11 language-lawyer






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 7:04







Simon Richter

















asked Mar 7 at 6:37









Simon RichterSimon Richter

21.8k13352




21.8k13352







  • 1





    How do you use this operator? Could you perhaps show us a Minimal, Complete, and Verifiable example that exhibits the problem you have? Even if we can't replicate the problem, it could be useful to see how you use it. And it could be very useful to see your implementation of the member overload, and what othertype is.

    – Some programmer dude
    Mar 7 at 6:43












  • By the way, do you really need to non-explicit conversion operator? What happens if you remove it or make it explicit?

    – Some programmer dude
    Mar 7 at 6:56











  • @Someprogrammerdude, this is part of a refactoring — mytype is supposed to replace othertype in the long run, so it needs to be convertible to allow gradually replacing instances without breaking the rest of the program.

    – Simon Richter
    Mar 7 at 7:05












  • 1





    How do you use this operator? Could you perhaps show us a Minimal, Complete, and Verifiable example that exhibits the problem you have? Even if we can't replicate the problem, it could be useful to see how you use it. And it could be very useful to see your implementation of the member overload, and what othertype is.

    – Some programmer dude
    Mar 7 at 6:43












  • By the way, do you really need to non-explicit conversion operator? What happens if you remove it or make it explicit?

    – Some programmer dude
    Mar 7 at 6:56











  • @Someprogrammerdude, this is part of a refactoring — mytype is supposed to replace othertype in the long run, so it needs to be convertible to allow gradually replacing instances without breaking the rest of the program.

    – Simon Richter
    Mar 7 at 7:05







1




1





How do you use this operator? Could you perhaps show us a Minimal, Complete, and Verifiable example that exhibits the problem you have? Even if we can't replicate the problem, it could be useful to see how you use it. And it could be very useful to see your implementation of the member overload, and what othertype is.

– Some programmer dude
Mar 7 at 6:43






How do you use this operator? Could you perhaps show us a Minimal, Complete, and Verifiable example that exhibits the problem you have? Even if we can't replicate the problem, it could be useful to see how you use it. And it could be very useful to see your implementation of the member overload, and what othertype is.

– Some programmer dude
Mar 7 at 6:43














By the way, do you really need to non-explicit conversion operator? What happens if you remove it or make it explicit?

– Some programmer dude
Mar 7 at 6:56





By the way, do you really need to non-explicit conversion operator? What happens if you remove it or make it explicit?

– Some programmer dude
Mar 7 at 6:56













@Someprogrammerdude, this is part of a refactoring — mytype is supposed to replace othertype in the long run, so it needs to be convertible to allow gradually replacing instances without breaking the rest of the program.

– Simon Richter
Mar 7 at 7:05





@Someprogrammerdude, this is part of a refactoring — mytype is supposed to replace othertype in the long run, so it needs to be convertible to allow gradually replacing instances without breaking the rest of the program.

– Simon Richter
Mar 7 at 7:05












1 Answer
1






active

oldest

votes


















1














You might restrict your template with SFINAE to discard operator+<mytype>(mytype const &lhs, mytype const &rhs):



template<typename T, typename std::enable_if<!std::is_same<T, mytype>::value, int>::type = 0>
constexpr auto operator+(T const &lhs, mytype const &rhs) -> decltype(rhs + lhs)

return rhs + lhs;



Demo






share|improve this answer






















    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%2f55037500%2ftemplate-implementation-for-commutative-operators-legal%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









    1














    You might restrict your template with SFINAE to discard operator+<mytype>(mytype const &lhs, mytype const &rhs):



    template<typename T, typename std::enable_if<!std::is_same<T, mytype>::value, int>::type = 0>
    constexpr auto operator+(T const &lhs, mytype const &rhs) -> decltype(rhs + lhs)

    return rhs + lhs;



    Demo






    share|improve this answer



























      1














      You might restrict your template with SFINAE to discard operator+<mytype>(mytype const &lhs, mytype const &rhs):



      template<typename T, typename std::enable_if<!std::is_same<T, mytype>::value, int>::type = 0>
      constexpr auto operator+(T const &lhs, mytype const &rhs) -> decltype(rhs + lhs)

      return rhs + lhs;



      Demo






      share|improve this answer

























        1












        1








        1







        You might restrict your template with SFINAE to discard operator+<mytype>(mytype const &lhs, mytype const &rhs):



        template<typename T, typename std::enable_if<!std::is_same<T, mytype>::value, int>::type = 0>
        constexpr auto operator+(T const &lhs, mytype const &rhs) -> decltype(rhs + lhs)

        return rhs + lhs;



        Demo






        share|improve this answer













        You might restrict your template with SFINAE to discard operator+<mytype>(mytype const &lhs, mytype const &rhs):



        template<typename T, typename std::enable_if<!std::is_same<T, mytype>::value, int>::type = 0>
        constexpr auto operator+(T const &lhs, mytype const &rhs) -> decltype(rhs + lhs)

        return rhs + lhs;



        Demo







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 7 at 9:55









        Jarod42Jarod42

        119k12104189




        119k12104189





























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55037500%2ftemplate-implementation-for-commutative-operators-legal%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

            1928 у кіно

            Захаров Федір Захарович

            Ель Греко