“multiple overloads” using templated class with duplicate types

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
7
down vote

favorite
1












I'm trying to write a class that takes two templated types. This class inherits from an interface. See the below code.



#include <iostream>
#include <string>

template <typename T>
class IObserver
public:
virtual void Next(const T& value) noexcept = 0;
;

template <typename T1, typename T2>
class BinaryObserver : public IObserver<T1>, public IObserver<T2>
public:
void Next(const T1& value) noexcept override;

void Next(const T2& value) noexcept override;
;

int main()
// This is OK
BinaryObserver<int, float> mc1;
mc1.Next(0);
mc1.Next(0.0f);

// This fails to compile with "multiple overloads"
BinaryObserver<int, int> mc2;
mc2.Next(0);
mc2.Next(0);



I'm having trouble when T1 is the same type as T2. Obviously this means two Next functions will be generated with the same type, which gives an error: multiple overloads of 'Next' instantiate to the same signature.



What's an idiomatic way of fixing this? I'm not sure how to handle the case when T1=T2 since I'd only need one Next function generated



Thanks!










share|improve this question























  • It's not just the multiple overloads (which you can fix with some enable_if magic) but you are also deriving twice from the same base class which is also not allowed. @VTT's answer fixes both issues.
    – Alan Birtles
    1 hour ago














up vote
7
down vote

favorite
1












I'm trying to write a class that takes two templated types. This class inherits from an interface. See the below code.



#include <iostream>
#include <string>

template <typename T>
class IObserver
public:
virtual void Next(const T& value) noexcept = 0;
;

template <typename T1, typename T2>
class BinaryObserver : public IObserver<T1>, public IObserver<T2>
public:
void Next(const T1& value) noexcept override;

void Next(const T2& value) noexcept override;
;

int main()
// This is OK
BinaryObserver<int, float> mc1;
mc1.Next(0);
mc1.Next(0.0f);

// This fails to compile with "multiple overloads"
BinaryObserver<int, int> mc2;
mc2.Next(0);
mc2.Next(0);



I'm having trouble when T1 is the same type as T2. Obviously this means two Next functions will be generated with the same type, which gives an error: multiple overloads of 'Next' instantiate to the same signature.



What's an idiomatic way of fixing this? I'm not sure how to handle the case when T1=T2 since I'd only need one Next function generated



Thanks!










share|improve this question























  • It's not just the multiple overloads (which you can fix with some enable_if magic) but you are also deriving twice from the same base class which is also not allowed. @VTT's answer fixes both issues.
    – Alan Birtles
    1 hour ago












up vote
7
down vote

favorite
1









up vote
7
down vote

favorite
1






1





I'm trying to write a class that takes two templated types. This class inherits from an interface. See the below code.



#include <iostream>
#include <string>

template <typename T>
class IObserver
public:
virtual void Next(const T& value) noexcept = 0;
;

template <typename T1, typename T2>
class BinaryObserver : public IObserver<T1>, public IObserver<T2>
public:
void Next(const T1& value) noexcept override;

void Next(const T2& value) noexcept override;
;

int main()
// This is OK
BinaryObserver<int, float> mc1;
mc1.Next(0);
mc1.Next(0.0f);

// This fails to compile with "multiple overloads"
BinaryObserver<int, int> mc2;
mc2.Next(0);
mc2.Next(0);



I'm having trouble when T1 is the same type as T2. Obviously this means two Next functions will be generated with the same type, which gives an error: multiple overloads of 'Next' instantiate to the same signature.



What's an idiomatic way of fixing this? I'm not sure how to handle the case when T1=T2 since I'd only need one Next function generated



Thanks!










share|improve this question















I'm trying to write a class that takes two templated types. This class inherits from an interface. See the below code.



#include <iostream>
#include <string>

template <typename T>
class IObserver
public:
virtual void Next(const T& value) noexcept = 0;
;

template <typename T1, typename T2>
class BinaryObserver : public IObserver<T1>, public IObserver<T2>
public:
void Next(const T1& value) noexcept override;

void Next(const T2& value) noexcept override;
;

int main()
// This is OK
BinaryObserver<int, float> mc1;
mc1.Next(0);
mc1.Next(0.0f);

// This fails to compile with "multiple overloads"
BinaryObserver<int, int> mc2;
mc2.Next(0);
mc2.Next(0);



I'm having trouble when T1 is the same type as T2. Obviously this means two Next functions will be generated with the same type, which gives an error: multiple overloads of 'Next' instantiate to the same signature.



What's an idiomatic way of fixing this? I'm not sure how to handle the case when T1=T2 since I'd only need one Next function generated



Thanks!







c++ c++11 templates overloading






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 hours ago

























asked 2 hours ago









Sam P

1,411824




1,411824











  • It's not just the multiple overloads (which you can fix with some enable_if magic) but you are also deriving twice from the same base class which is also not allowed. @VTT's answer fixes both issues.
    – Alan Birtles
    1 hour ago
















  • It's not just the multiple overloads (which you can fix with some enable_if magic) but you are also deriving twice from the same base class which is also not allowed. @VTT's answer fixes both issues.
    – Alan Birtles
    1 hour ago















It's not just the multiple overloads (which you can fix with some enable_if magic) but you are also deriving twice from the same base class which is also not allowed. @VTT's answer fixes both issues.
– Alan Birtles
1 hour ago




It's not just the multiple overloads (which you can fix with some enable_if magic) but you are also deriving twice from the same base class which is also not allowed. @VTT's answer fixes both issues.
– Alan Birtles
1 hour ago












1 Answer
1






active

oldest

votes

















up vote
9
down vote













How about a specialization:



template <typename T>
class BinaryObserver<T, T> : public IObserver<T>
public:
void Next(const T & value) noexcept override;
;





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',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    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%2f52458675%2fmultiple-overloads-using-templated-class-with-duplicate-types%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    9
    down vote













    How about a specialization:



    template <typename T>
    class BinaryObserver<T, T> : public IObserver<T>
    public:
    void Next(const T & value) noexcept override;
    ;





    share|improve this answer
























      up vote
      9
      down vote













      How about a specialization:



      template <typename T>
      class BinaryObserver<T, T> : public IObserver<T>
      public:
      void Next(const T & value) noexcept override;
      ;





      share|improve this answer






















        up vote
        9
        down vote










        up vote
        9
        down vote









        How about a specialization:



        template <typename T>
        class BinaryObserver<T, T> : public IObserver<T>
        public:
        void Next(const T & value) noexcept override;
        ;





        share|improve this answer












        How about a specialization:



        template <typename T>
        class BinaryObserver<T, T> : public IObserver<T>
        public:
        void Next(const T & value) noexcept override;
        ;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 hours ago









        VTT

        21.5k32143




        21.5k32143



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52458675%2fmultiple-overloads-using-templated-class-with-duplicate-types%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            Installing NextGIS Connect into QGIS 3?

            One-line joke