473,385 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

templating constness

Hi
I wonder if it is possible to have templates expand based on constness,
something like:

template<typename A, typename B>copy( A &a, B &b);

becoming:

copy( const class AClass &a, class BClass &b );

or

copy( class AClass &a, const class BClass &b );

based on arguments.

The purpose of this would be to be able to write something like

template<typename A, typename B>copy( A &a, B &b)
{
copy( a.name, b.name);
copy( a.value, b.value)
}

And have the constness of the arguments decide the direction.

Adding a third parameter specifying direction would be OK.
The main thing is to be able to use consting without const_cast's.

Thanks
Olav

Nov 17 '05 #1
7 1011
On Tue, 20 Jul 2004 11:03:48 +0200, "Olav"
<Ol**************@hotmail.com> wrote:
Hi
I wonder if it is possible to have templates expand based on constness,
something like:

template<typename A, typename B>copy( A &a, B &b);

becoming:

copy( const class AClass &a, class BClass &b );

or

copy( class AClass &a, const class BClass &b );

based on arguments.

The purpose of this would be to be able to write something like

template<typename A, typename B>copy( A &a, B &b)
{
copy( a.name, b.name);
copy( a.value, b.value)
}

And have the constness of the arguments decide the direction.
That sounds dangerous to me. Doesn't the caller of copy know the
direction? They should!

Adding a third parameter specifying direction would be OK.
The main thing is to be able to use consting without const_cast's.


Overloading would do it I think:

template <typename A, typename B>
void copy_lhs_to_rhs(A const& a, B& b); //implement however
template<typename A, typename B>
inline void copy( A &a, B const&b)
{
copy_lhs_to_rhs(b, a);
}

template<typename A, typename B>
inline void copy( A const& a, B& b)
{
copy_lhs_to_rhs(a, b);
}

That also nicely will report an ambiguity if you try to copy two
non-const objects, and an error if you try to copy two const objects.

However, I don't quite see the point of this - you always know which
way you're copying.

Tom
Nov 17 '05 #2

"tom_usenet" <to********@hotmail.com> wrote in message
news:er********************************@4ax.com...
On Tue, 20 Jul 2004 11:03:48 +0200, "Olav"
<Ol**************@hotmail.com> wrote:
Thanks, will have a look - it might work!
However, I don't quite see the point of this - you always know which
way you're copying.

Tom

Its not really copying - its serialization and de-serialization, and its
very practical
to have every class mapped in one method instead of two.

Olav



Nov 17 '05 #3
"tom_usenet" <to********@hotmail.com> wrote in message
news:er********************************@4ax.com...
On Tue, 20 Jul 2004 11:03:48 +0200, "Olav"
<Ol**************@hotmail.com> wrote:
Hi
I wonder if it is possible to have templates expand based on constness,
something like:
template<typename A, typename B>
inline void copy( A &a, B const&b)
{
copy_lhs_to_rhs(b, a);
}

Think it would fail here, as the two types are different.
The best solution I have found so far is:

void Process(A &a, XMLNode &xmlNode, LoadStoreAction action )

With templates doing deconst-casts when necesarry.

The second type is actually always XMLNode (though ideally with and without
const),
while the left side is different classes

Thanks
Olav


Nov 17 '05 #4
On Thu, 22 Jul 2004 14:23:30 +0200, "Olav"
<Ol**************@hotmail.com> wrote:
"tom_usenet" <to********@hotmail.com> wrote in message
news:er********************************@4ax.com.. .
On Tue, 20 Jul 2004 11:03:48 +0200, "Olav"
<Ol**************@hotmail.com> wrote:
>Hi
>I wonder if it is possible to have templates expand based on constness,
>something like:

template<typename A, typename B>
inline void copy( A &a, B const&b)
{
copy_lhs_to_rhs(b, a);
}

Think it would fail here, as the two types are different.


Why would that make it fail? copy_lhs_to_rhs is a templated function
that can take any pair of types, and template argument deduction will
deduce the correct types for A and B there (and they will be A=copy's
B and B=copy's A!)

Tom
Nov 17 '05 #5

"tom_usenet" <to********@hotmail.com> wrote in message
news:ph********************************@4ax.com...
On Thu, 22 Jul 2004 14:23:30 +0200, "Olav"

Think it would fail here, as the two types are different.


Why would that make it fail? copy_lhs_to_rhs is a templated function
that can take any pair of types, and template argument deduction will
deduce the correct types for A and B there (and they will be A=copy's
B and B=copy's A!)

Tom


The code might be correct, but the purpose of the exercise is to have only
one function pr. class.
I think with your code I would have to implement, for a class A:

void copy_lhs_to_rhs(A const& a, XMLNode & xmlNode);

and

void copy_lhs_to_rhs(const XMLNode & xmlNode, A & a );

Disagree?

Nov 17 '05 #6
On Thu, 22 Jul 2004 16:59:46 +0200, "Olav"
<Ol**************@hotmail.com> wrote:

"tom_usenet" <to********@hotmail.com> wrote in message
news:ph********************************@4ax.com.. .
On Thu, 22 Jul 2004 14:23:30 +0200, "Olav"

>Think it would fail here, as the two types are different.


Why would that make it fail? copy_lhs_to_rhs is a templated function
that can take any pair of types, and template argument deduction will
deduce the correct types for A and B there (and they will be A=copy's
B and B=copy's A!)

Tom


The code might be correct, but the purpose of the exercise is to have only
one function pr. class.
I think with your code I would have to implement, for a class A:

void copy_lhs_to_rhs(A const& a, XMLNode & xmlNode);

and

void copy_lhs_to_rhs(const XMLNode & xmlNode, A & a );

Disagree?


Ahh, so you do need a "direction" parameter of some kind, and
appropriate const_casts. I'm not convinced that having one shared
function is going to improve your code at all though. In the past I've
tried this kind of thing and ended up going back to two functions just
to get rid of all the const_casts and because only the read function
needed to be virtual. Any code that contains const_cast is obviously
error prone, particularly if the const_cast happened in a different
function.

Tom
Nov 17 '05 #7


"tom_usenet" <to********@hotmail.com> wrote in message
news:<d2********************************@4ax.com>. ..
On Thu, 22 Jul 2004 16:59:46 +0200, "Olav"
<Ol**************@hotmail.com> wrote:
I think with your code I would have to implement, for a class A:

void copy_lhs_to_rhs(A const& a, XMLNode & xmlNode);

and

void copy_lhs_to_rhs(const XMLNode & xmlNode, A & a );

Disagree?


Ahh, so you do need a "direction" parameter of some kind, and
appropriate const_casts. I'm not convinced that having one shared
function is going to improve your code at all though. In the past I've
tried this kind of thing and ended up going back to two functions just
to get rid of all the const_casts and because only the read function
needed to be virtual. Any code that contains const_cast is obviously
error prone, particularly if the const_cast happened in a different
function.

The const casting can be done in a few templates (not pr class), with an
assert on direction it is quite safe.

Also, the problem can be solved by passing around two refs: one const and
one non-const:

template<class TDomainClass>
void Process(
TDomainClass *obj_t,
TDomainClass *const_obj_t,
XMLNode *xmlNode,
// XMLNode *const_xmlNode,
LoadStoreAction action)
{

Pointers instead of refs as at least the non-const parameters might be null.

Olav


"tom_usenet" <to********@hotmail.com> wrote in message
news:d2********************************@4ax.com... On Thu, 22 Jul 2004 16:59:46 +0200, "Olav"
<Ol**************@hotmail.com> wrote:

"tom_usenet" <to********@hotmail.com> wrote in message
news:ph********************************@4ax.com.. .
On Thu, 22 Jul 2004 14:23:30 +0200, "Olav"

>Think it would fail here, as the two types are different.

Why would that make it fail? copy_lhs_to_rhs is a templated function
that can take any pair of types, and template argument deduction will
deduce the correct types for A and B there (and they will be A=copy's
B and B=copy's A!)

Tom


The code might be correct, but the purpose of the exercise is to have onlyone function pr. class.
I think with your code I would have to implement, for a class A:

void copy_lhs_to_rhs(A const& a, XMLNode & xmlNode);

and

void copy_lhs_to_rhs(const XMLNode & xmlNode, A & a );

Disagree?


Ahh, so you do need a "direction" parameter of some kind, and
appropriate const_casts. I'm not convinced that having one shared
function is going to improve your code at all though. In the past I've
tried this kind of thing and ended up going back to two functions just
to get rid of all the const_casts and because only the read function
needed to be virtual. Any code that contains const_cast is obviously
error prone, particularly if the const_cast happened in a different
function.

Tom

Nov 17 '05 #8

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

Similar topics

18
by: pkassianidis | last post by:
Hello everybody, I am in the process of writing my very first web application in Python, and I need a way to generate dynamic HTML pages with data from a database. I have to say I am...
7
by: Paul | last post by:
I have been coding apps with PHP for several years. But I am getting more involved in larger and more complicated PHP applications and want to use best practices. I use Eclipse's PHP IDE. I...
13
by: Javier | last post by:
Hello, I have some cuestions about constness with standard containers and pointers. Supose I have a list of pointers to some class B: std::list< B * list; I have readed that constness in...
10
by: Terrence Brannon | last post by:
Hello, The most common way of dynamically producing HTML is via template engines like genshi, cheetah, makotemplates, etc. These engines are 'inline' --- they intersperse programming...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...

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.