Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Template overriding of operator []

Template overriding of operator []

Scheduled Pinned Locked Moved C / C++ / MFC
wpfcomhelptutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alexandru Savescu
    wrote on last edited by
    #1

    Hello, I have a class and I want to override its [] operator. So I did like this:

    int operator [] (LPCTSTR x)
    {
    .....
    }

    Now I want my operator to return long, doubles, strings etc. I don't want to manually write dozen of operators, but handle it with templates:

    template <typename T>
    T operator [] (LPCTSTR x)
    {
    T t;
    ....
    return t;
    }

    This complies fine. My problem is that I don't know how to call my template operator. I tried:

    MyClass m;
    m["str"]<int>;
    m<int>["str"];
    m.operator <int>[] ("str");

    but all I got were compiler errors. Any ideas? Thanks Best regards, Alexandru Savescu P.S. Interested in art? Visit this!

    J 1 Reply Last reply
    0
    • A Alexandru Savescu

      Hello, I have a class and I want to override its [] operator. So I did like this:

      int operator [] (LPCTSTR x)
      {
      .....
      }

      Now I want my operator to return long, doubles, strings etc. I don't want to manually write dozen of operators, but handle it with templates:

      template <typename T>
      T operator [] (LPCTSTR x)
      {
      T t;
      ....
      return t;
      }

      This complies fine. My problem is that I don't know how to call my template operator. I tried:

      MyClass m;
      m["str"]<int>;
      m<int>["str"];
      m.operator <int>[] ("str");

      but all I got were compiler errors. Any ideas? Thanks Best regards, Alexandru Savescu P.S. Interested in art? Visit this!

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      The syntax for calling your templatized operator is as convoluted as it can get:

      m.template operator[]<int>("str");

      Unfortunately, VC++ 6.0sp5 does not swallow this (don't know for other MS compilers). An alternative, that also simplifies notation, is as follows:

      template <typename T>
      struct Type2Type // borrowed from Alexandrescu's "Modern C++ Design"
      {
      typedef T type;
      };

      struct MyClass
      {
      template <typename T> T operator () (const char * x,Type2Type<T>)
      {
      T t;
      ...
      return t;
      }
      };

      int main()
      {
      MyClass m;
      m("str",Type2Type<int>());
      m("str",Type2Type<double>());
      return 0;
      }

      This solution alas drops the nice [] notation in favor of simpler parentheses, but I don't think you have a better alternative. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups