473,722 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to cause const promotion for template code

Hi. I have a function

template <class InputIter, class OutputIter>
void f(InputIter begin, InputIter end, OutputIter result);

With c of type char* and cc of type const char*, the code f(c,c,cc) calls
f<char*, const char *>, which is fine.

But f(c,c,c) calls a new instantiation f<char*,char*> whereas I'd like it to
call f<const char*,char*>.

How to make this happen?
Jul 22 '05 #1
7 1839
Siemel Naran wrote in
news:_%******** ***********@bgt nsc05-news.ops.worldn et.att.net in
comp.lang.c++:
Hi. I have a function

template <class InputIter, class OutputIter>
void f(InputIter begin, InputIter end, OutputIter result);

With c of type char* and cc of type const char*, the code f(c,c,cc)
calls f<char*, const char *>, which is fine.
Well a "char const *" *isn't* an output iterator, which isn't my
defenition of "fine".

But f(c,c,c) calls a new instantiation f<char*,char*> whereas I'd like
it to call f<const char*,char*>.
Why, what difference does it make ?. If you're trying to avoid code bloat
some modern compilers come with optimizers that can do this for you.

How to make this happen?


template < typename T >
inline void f( T *a, T *b, T *c )
{
f< T const *, T * >( a, b, c );
}

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2
"Rob Williscroft" <rt*@freenet.co .uk> wrote in message
Siemel Naran wrote in
template <class InputIter, class OutputIter>
void f(InputIter begin, InputIter end, OutputIter result);

With c of type char* and cc of type const char*, the code f(c,c,cc)
calls f<char*, const char *>, which is fine.


Well a "char const *" *isn't* an output iterator, which isn't my
defenition of "fine".


Oops. That should have been f(cc,cc,c) which calls f<const char*, char*>
which is fine.
Why, what difference does it make ?. If you're trying to avoid code bloat
some modern compilers come with optimizers that can do this for you.
But some don't, so I have to do it.

template < typename T >
inline void f( T *a, T *b, T *c )
{
f< T const *, T * >( a, b, c );
}


First, can one specialize member functions? My function f is actually a
member function.

Second, how to handle STL iterators. For example, I want
f<std::deque<in t>::iterator, std::deque<int> ::iterator> to call
f<std::deque<in t>::const_itera tor, std::deque<int> ::iterator>. Do you know
how to achieve this?

Thanks.
Jul 22 '05 #3
Siemel Naran wrote in
news:wm******** ***********@bgt nsc05-news.ops.worldn et.att.net in
comp.lang.c++:
Why, what difference does it make ?. If you're trying to avoid code
bloat some modern compilers come with optimizers that can do this for
you.
But some don't, so I have to do it.


Do you know this for a fact, have you measured a performance drop
because of it ?

template < typename T >
inline void f( T *a, T *b, T *c )
{
f< T const *, T * >( a, b, c );
}
First, can one specialize member functions? My function f is actually
a member function.


The above isn't a specialization its an overload, and yes you can
overload member functions even when they are templates.

Also you can only explicitly specialze functions. eg:

template < typename T > void f( T ) {}
template <> void f< int >( int ) {}

Second, how to handle STL iterators. For example, I want
f<std::deque<in t>::iterator, std::deque<int> ::iterator> to call
f<std::deque<in t>::const_itera tor, std::deque<int> ::iterator>. Do you
know how to achieve this?


It can't be done as there is now way of getting a const_iterator
given *only* an iterator (except when the iterator is a pointer).

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4
Siemel Naran wrote:
"Rob Williscroft" <rt*@freenet.co .uk> wrote in message
template < typename T >
inline void f( T *a, T *b, T *c )
{
f< T const *, T * >( a, b, c );
}
First, can one specialize member functions? My function f is

actually a member function.
How is this question related to the above function? This is not a
specialization but an overload and you surely can overload member
functions. Other than this, the answer to your question is: no.
Actually, you can only fully specialize global functions while you
would require a partial specialization anyway.
Second, how to handle STL iterators. For example, I want
f<std::deque<in t>::iterator, std::deque<int> ::iterator> to call
f<std::deque<in t>::const_itera tor, std::deque<int> ::iterator>.


Since there is no requirement that an iterator has a constant
counterpart, there is no requirement for something like
'const_iterator ' in iterator traits. You might get away by
using your own traits class:

template <typename It> struct const_it { typedef It type; };
template <typename T> struct const_it<T*> { typedef T const* type; };
template <typename T, typename A>
struct const_it<std::d eque<T, A>::iterator> {
typedef std::deque<T, A>::const_itera tor type;
};
// ...

.... and use this in your function's implementation:

template <typename InputIter, typename OutputIter>
OutputIter f(InputIter beg, InputIter end, OutputIter to) {
return f<typename const_it<InputI ter>::type, OutputIter>(beg , end,
to);
}

A complication is that 'std::vector<T> 's iterator may actually be
'T*' in which case you would attempt to specialize twice for the
type 'T*' if you added vector's iterator to the above specialization.
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting

Jul 22 '05 #5
Siemel Naran wrote:
"Rob Williscroft" <rt*@freenet.co .uk> wrote in message
template < typename T >
inline void f( T *a, T *b, T *c )
{
f< T const *, T * >( a, b, c );
}
First, can one specialize member functions? My function f is

actually a member function.
How is this question related to the above function? This is not a
specialization but an overload and you surely can overload member
functions. Other than this, the answer to your question is: no.
Actually, you can only fully specialize global functions while you
would require a partial specialization anyway.
Second, how to handle STL iterators. For example, I want
f<std::deque<in t>::iterator, std::deque<int> ::iterator> to call
f<std::deque<in t>::const_itera tor, std::deque<int> ::iterator>.


Since there is no requirement that an iterator has a constant
counterpart, there is no requirement for something like
'const_iterator ' in iterator traits. You might get away by
using your own traits class:

template <typename It> struct const_it { typedef It type; };
template <typename T> struct const_it<T*> { typedef T const* type; };
template <typename T, typename A>
struct const_it<std::d eque<T, A>::iterator> {
typedef std::deque<T, A>::const_itera tor type;
};
// ...

.... and use this in your function's implementation:

template <typename InputIter, typename OutputIter>
OutputIter f(InputIter beg, InputIter end, OutputIter to) {
return f<typename const_it<InputI ter>::type, OutputIter>(beg , end,
to);
}

A complication is that 'std::vector<T> 's iterator may actually be
'T*' in which case you would attempt to specialize twice for the
type 'T*' if you added vector's iterator to the above specialization.
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting

Jul 22 '05 #6
"Dietmar Kuehl" <di***********@ yahoo.com> wrote in message
Siemel Naran wrote:
First, can one specialize member functions? My function f is

How is this question related to the above function? This is not a
specialization but an overload and you surely can overload member
functions. Other than this, the answer to your question is: no.
Why can one not specialize member functions?

Since there is no requirement that an iterator has a constant
counterpart, there is no requirement for something like
'const_iterator ' in iterator traits. You might get away by
using your own traits class:

template <typename It> struct const_it { typedef It type; };
template <typename T> struct const_it<T*> { typedef T const* type; };
template <typename T, typename A>
struct const_it<std::d eque<T, A>::iterator> {
typedef std::deque<T, A>::const_itera tor type;
};
// ...

... and use this in your function's implementation:

template <typename InputIter, typename OutputIter>
OutputIter f(InputIter beg, InputIter end, OutputIter to) {
return f<typename const_it<InputI ter>::type, OutputIter>(beg , end,
to);
}

A complication is that 'std::vector<T> 's iterator may actually be
'T*' in which case you would attempt to specialize twice for the
type 'T*' if you added vector's iterator to the above specialization.


Yes, this is a good idea. BTW, is the following legal in a fully ANSI
compliant compiler

template <typename Container>
struct const_it<typena me Container::iter ator> {
typedef typename Container::cons t_iterator type;
};

I guess not, because the compiler might have a hard time deducing
'Container' given a specialization const_it<MyIter >.
But it gets me thinking to a new solution.

template <class Container, class InputIter, class OutputIter>
void f(const Container&, typename Container::cons t_iterator begin, typename
Container::cons t_iterator end, OutputIter result);

We explicitly pass in the container in order that we can use
Container::cons t_iterator. There will be a specialization

template <class T, class OutputIter>
void f<const T*,const T*, OutputIter>(con st T*, const T* begin, const T*
end, OutputIter result);
Jul 22 '05 #7
Siemel Naran wrote:
Why can one not specialize member functions?
Because it is sufficient to overload them.
BTW, is the following legal in a fully ANSI compliant compiler

template <typename Container>
struct const_it<typena me Container::iter ator> {
typedef typename Container::cons t_iterator type;
};
No: the container cannot be deduced if two containers share a common
iterator type. Still, the typedefs of related types may differ
for the two containers. The same is true for argument type deduction
in function templates: you cannot use a nested type.
But it gets me thinking to a new solution.

template <class Container, class InputIter, class OutputIter>
void f(const Container&, typename Container::cons t_iterator begin, typename Container::cons t_iterator end, OutputIter result);


In this case, I'd go with a "range" which would be a pair of member
function ('begin()' and 'end()'; optionally also 'rbegin()' and
'rend()' for reversible ranges) plus a set of supporting typedefs.
The function would than just get one parameter for the input sequence,
namely the range.
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting

Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2207
by: Chris Johnson | last post by:
Greetings all: I come across an interesting question (to me anyway) and I do not know the answer. Code/Questions follow: #include <iostream> #if 0 // uncommenting *should* make call ambigous because of // odering deduction of the function signature,
0
1518
by: Prune Tracy | last post by:
Hello. As practice in programming using templates, and for interest's sake, I wrote some code to see how floating point numbers are stored on my system. I hoped to be able to use it like: std::cout << internal_rep(1.0F) << std::endl; std::cout << internal_rep(1.0) << std::endl; and it would give me a hexadecimal output of the bytes making up the storage of 1.0 in float and double format.
3
1984
by: Michael T Decerbo | last post by:
The code below compiles without warnings or errors under g++ 3.2.1 on Solaris, gcc 3.2.2 on Linux, and MSVC 6.0 on Windows. Under g++ (GCC) 3.3.2, it produces the following errors: % g++ -Wall testme_std.cpp testme_std.cpp:24: error: no `I LblID<I, MaxInd>::add(const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)' member function declared in class `LblID<I, MaxInd>' testme_std.cpp:24: error: template...
2
4064
by: Steve | last post by:
Hi all, Is template code "always" inlined??? Or will compilers instantiate template functions separately if they are large? Also, what happens to the code if explicit instantiation mechanism is used? ] Are template functions, objects and member functions then placed as separate entities within the object file, as with any non templated
0
1103
by: gao_bolin | last post by:
I have a library that has a class Signal<T> whose data type is templated. The class comes with a whole bunch of functions and objects to do various processing on the signal. What I would like to do is a DSignal class that does essentially the same thing, but whose type is unknown at compile type. Since I have already this template library with everything I want in it, I would like to maximally reuse it and minimize overheads. It's the...
10
17567
by: dee | last post by:
Hi How can I cause a postback in code, e.g., without clicking on a button? Thanks. Dee
0
974
by: Jack | last post by:
Hi, I want to override an overridable method in some base class from my class. It works fine if I manually code the method (as one would expect), but I want the IDE to auto-generate the template code for me so I don't have to type all of it in. I'm sure this is possible. Apparently you can do it from the class designer somehow by openning the class designer, drilling up through the inheritence heirarchy, choosing the method ( eg....
0
1383
by: Thelma Roslyn Lubkin | last post by:
dizzy <dizzy@roedu.netwrote: : Hello : When working with template heavy code one will have to face the : inherent "problems" of the inclusion model because of the requirement : to provide the definition of the template at every place where it is : used. This means problems with include separation and problems with : code duplication (the later depends largely on your toolchain but : unless I am missing out on a modern standard compliant...
4
1596
by: Pallav singh | last post by:
Hi All, i am getting error during explicit function Instantiation for a class Template if i do explicit Instantiation of class it work and all function symbol i get in object file But if i try to expose only one function of my class its failing #include <iostream>
0
8867
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9239
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8059
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6685
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4764
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2606
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2148
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.