Topic: Local classes and member templates (regarding new proposal)


Author: gdr@integrable-solutions.net (Gabriel Dos Reis)
Date: Thu, 2 Oct 2003 23:56:59 +0000 (UTC)
Raw View
fvali@kumc.edu ("Faisal Vali") writes:

[...]

| Does anyone else have a strong opinion on this topic?

Template basic questions: What would be the definition contexts, what
would be the instantiation contexts?

--
                                                       Gabriel Dos Reis
                                           gdr@integrable-solutions.net

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: fvali@kumc.edu ("Faisal Vali")
Date: Thu, 2 Oct 2003 22:13:11 +0000 (UTC)
Raw View
There is a proposal being considered by the standards committee whereby
local classes will have their linkage modified so that they can be used
to instantiate templates.

The proposal forbids the local classes from either being templates
themselves or having member templates.

I believe this is a limitation that should be removed since having
member templates is a convenience that allows us to avoid writing
complicated types in the function declarations, and also saves us from
duplicate typing when the bodies are very similar but the parameter
types are not (the basic motivation behind templates of course).

I present as motivation, an actual example that was authored by someone
on comp.lang.c++.moderated (which i have modified to use local templates
and added a use with a multi-dim char array)

The following is just one example - but I do hope the committee will
re-consider the limitation on creation of local templates and member
templates in local classes.

Does anyone else have a strong opinion on this topic?

/// The example:

int main()
{

  struct accumulate
  {
    template<typename T1, typename T2, size_t N>
    T1 operator() (T1 init, T2 (&array)[N]) {
      return std::accumulate (array, array + N, init);
    }

    template<typename T1, size_t N, typename T2, typename F>
    T2 operator() (T1 (&array)[N], T2 init, F fn) {
      return std::accumulate (array, array + N, init, fn);
    }
  };

  int a[][5] = {
    {1, 2, 3, 4, 5}
    , {2, 3, 4, 5, 6}
  };

  char b[][10] = { .... };

  int sum = accumulate() (a, 0, accumulate());
  sum += accumulate()(b, 0, accumulate());
}


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]