C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Deduction of not-type template parameter type

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Vidar Hasfjord
Guest





PostPosted: Mon Apr 16, 2007 2:41 pm    Post subject: Deduction of not-type template parameter type Reply with quote



Sometimes it would be convenient to allow a function argument to be
used as a template argument. For example, consider a factory function
that generates lookup functions (the example is inspired by James
Kanze's thread "Incomplete type in template parameter, complete type
as argument"):

typedef bool (*contains_fun) (const char* key);

template <size_t N, const char* (&Keys) [N]>
bool contains (const char* key) {
return std::find_if (Keys, Keys + N, Match (key))
!= Keys + N;
}

template <size_t N>
contains_fun
make_contains (const char* (&keys) [N]) {
return &contains <N, keys>;
}

C++03 doesn't allow this, of course, but with the addition of
constexpr (N1980) in C++09 it seems feasible:

template <size_t N>
constexpr contains_fun
make_contains (constexpr const char* (&keys) [N]) {
return &contains <N, keys>;
}

By adding constexpr to the function parameter it is guaranteed that
the function will be evaluated at compile-time, which should allow the
passed compile-time constant argument to be used as a template
argument.

The passing of the array as an argument to the factory function allows
the size to be deduced, and forwarding the array to the "contains"
template parameter allows the array to be hard-coded within the
generated function.

In general this feature would allow function parameter type deduction
to be used to select the type of a non-type template parameter based
on the argument passed. AFAIK, there is no type deduction facility for
the type of a non-type template parameter equivalent to the deduction
of function parameter types. This feature would bridge that gap.


Vidar Hasfjord

---
[ 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.comeaucomputing.com/csc/faq.html ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.