Koenig's lookup for template argument.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peng Yu

    #1

    Koenig's lookup for template argument.

    Hi,

    I'm wondering if Koenig lookup can be applied somehow to derive which
    template to use based on the template arguments.

    The following code shows an example where multiply_traits 's namespace
    A has to be specified in the definition of Y. This makes Y unusable
    for any multiply_traits defined in other namespaces, which
    corresponding T1 and T2 defined in those namespaces. See also comments
    in the code.

    Thanks,
    Peng


    #include <iostream>

    namespace A {

    template <typename T>
    class X {
    public:
    X() { }
    X(T t) : _t(t) { }
    const T &the_t() const { return _t; }
    private:
    T _t;
    };

    template <typename T1, typename T2>
    struct multiply_traits ;

    template <typename T1, typename T2>
    struct multiply_traits <X<T1>, T2{
    typedef X<T1result_type ;
    };

    template <typename T1, typename T2>
    typename multiply_traits <X<T1>, T2>::result_typ e operator*(const
    X<T1&x, const T2 &t) {
    return X<T1>(x.the_t( ) * t);
    }

    }

    namespace B {

    template <typename T>
    class Y {
    public:
    Y(T t) : _t(t) { }
    const T &the_t() const { return _t; }
    private:
    T _t;
    };

    template <typename T1, typename T2>
    Y<typename A::multiply_tra its<T1, T2>::result_typ e>
    #if 0
    I want to specify the template argument without explicitly say the
    namespace A.
    If this could be possible, Y can be used for types that are defined
    in any namespace as long as a corresponding multiply_traits are
    defined in such namespace.
    But I just don't find such a way.
    Is there anybody know if there is any walkaround?
    #end if
    operator*(const Y<T1&y, const T2 &t) {
    return Y<T1>(y.the_t( ) * t);
    }
    }

    int main () {
    A::X<intx(2);
    B::Y<A::X<int y(x);

    std::cout << (x * 3).the_t() << std::endl;
    std::cout << (y * 5).the_t().the_ t() << std::endl;
    }
  • Hendrik Schober

    #2
    Re: Koenig's lookup for template argument.

    Peng Yu wrote:
    Hi,
    >
    I'm wondering if Koenig lookup can be applied somehow to derive which
    template to use based on the template arguments.
    What you do is compile-time stuff. Koenig lookup,
    OTOH, only applies to function parameters. So I
    see no way you can use this.
    [...]
    HTH,

    Schobi

    Comment

    • dascandy@gmail.com

      #3
      Re: Koenig's lookup for template argument.

      On Oct 20, 9:00 pm, Peng Yu <PengYu...@gmai l.comwrote:
        template <typename T1, typename T2>
          Y<typename A::multiply_tra its<T1, T2>::result_typ e>
      IIRC, there was a proposal for "auto" return types somewhere in c++0x.
      Also, boost has a template iirc that does just this - determine what
      the return value of some operation is. For these concrete problems
      those solutions might help you.

      As for the abstract problem of template importing from an unknown
      namespace, Koenig lookup doesn't apply. You need to specify the
      namespace one way or another.

      Comment

      Working...