Templates and Interfaces

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paulo Matos

    #1

    Templates and Interfaces

    Hi all,

    I'm curious if I can say in C++ that a certain template can only be
    instantiated (template-wise) by a class implementing a certain
    interface (inheriting from a given abstract class).

    Regards,

    Paulo Matos

  • Victor Bazarov

    #2
    Re: Templates and Interfaces

    Paulo Matos wrote:
    I'm curious if I can say in C++ that a certain template can only be
    instantiated (template-wise) by a class implementing a certain
    interface (inheriting from a given abstract class).
    There are several ways of doing that. Google for "is_base_of " or
    something like that. There are compile-time assertions for that
    kind of test.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Kai-Uwe Bux

      #3
      Re: Templates and Interfaces

      Paulo Matos wrote:
      I'm curious if I can say in C++ that a certain template can only be
      instantiated (template-wise) by a class implementing a certain
      interface (inheriting from a given abstract class).
      Sure you can say that: nobody is going to stop you. However, be informed,
      that if you did, you would be asserting something false: template
      instantiation in C++ is a concept totally independent from inheritance (be
      it from abstract base classes or from concrete classes).

      Maybe there is an underlying problem or confusion. What triggered your
      question?


      Best

      Kai-Uwe Bux

      Comment

      • Kai-Uwe Bux

        #4
        Re: Templates and Interfaces

        Kai-Uwe Bux wrote:
        Paulo Matos wrote:
        >
        >I'm curious if I can say in C++ that a certain template can only be
        >instantiated (template-wise) by a class implementing a certain
        >interface (inheriting from a given abstract class).
        >
        Sure you can say that: nobody is going to stop you. However, be informed,
        that if you did, you would be asserting something false: template
        instantiation in C++ is a concept totally independent from inheritance (be
        it from abstract base classes or from concrete classes).
        >
        Maybe there is an underlying problem or confusion. What triggered your
        question?
        Oh well, I guess I was confused: reading Victors reply, I realized that I
        profoundly misunderstood your question. Sorry about that.

        To make up for it, here is what I use to test for public inheritance:


        template < typename B, typename D >
        class is_subclass {

        typedef char (&no) [1];
        typedef char (&yes) [2];

        struct dummy {
        operator D& ( void );
        };

        static
        yes check ( B& );

        static
        no check ( ... );

        public:

        static bool const value = sizeof( check( dummy() ) ) == sizeof( yes );

        }; // is_subclass


        template < typename B, typename D >
        class is_a {

        typedef char (&no) [1];
        typedef char (&yes) [2];

        static
        D& dummy ( void );

        static
        yes check ( B& );

        static
        no check ( ... );

        public:

        static bool const value = sizeof( check( dummy() ) ) == sizeof( yes );

        }; // is_a


        struct ABC {};

        struct A : public ABC {};

        struct X {
        static A dummy;
        operator A& () {
        return dummy;
        }
        };
        A X::dummy;

        struct Y {
        static ABC dummy;
        operator ABC& ( void ) {
        return dummy;
        }
        };

        ABC Y::dummy;


        #include <iostream>

        int main ( void ) {
        std::cout
        << is_subclass<ABC ,ABC>::value << " "
        << is_subclass<ABC ,A>::value << " "
        << is_subclass<ABC ,X>::value << " "
        << is_subclass<ABC ,Y>::value << "\n"
        << is_a<ABC,ABC>:: value << " "
        << is_a<ABC,A>::va lue << " "
        << is_a<ABC,X>::va lue << " "
        << is_a<ABC,Y>::va lue << "\n";
        }


        Things to note:
        a) I consider every class is subclass of itself.
        b) is_a<is more permissive than is_subclass. (The key is that the struct
        dummy approach will prevent further user-defined conversions since we
        already used one.)
        c) These templates are not supposed to handle private inheritance.


        Best

        Kai-Uwe Bux

        Comment

        Working...