Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Logical Constness

Reply
Thread Tools

Logical Constness

 
 
Tarique
Guest
Posts: n/a
 
      06-25-2008
Can someone please explain the idea of Logical Constness as explained in
section 10.2.7.1 Of Stroustrup's "The C++ Programming Language" ,with a
short working code if possible.

Thank You
 
Reply With Quote
 
 
 
 
SeanW
Guest
Posts: n/a
 
      06-25-2008
On Jun 25, 2:40 pm, Tarique <peo_...@yahoo.com> wrote:
> Can someone please explain the idea of Logical Constness as explained in
> section 10.2.7.1 Of Stroustrup's "The C++ Programming Language" ,with a
> short working code if possible.
>
> Thank You


This article covers it: http://www.ddj.com/cpp/184403892

Sean
 
Reply With Quote
 
 
 
 
Tarique
Guest
Posts: n/a
 
      06-26-2008
SeanW wrote:
> On Jun 25, 2:40 pm, Tarique <peo_...@yahoo.com> wrote:
>> Can someone please explain the idea of Logical Constness as explained in
>> section 10.2.7.1 Of Stroustrup's "The C++ Programming Language" ,with a
>> short working code if possible.
>>
>> Thank You

>
> This article covers it: http://www.ddj.com/cpp/184403892
>
> Sean


I found this link when i Google'd it and i could not make much sense of
it! Phew!!! I am only a C++ newbie. Maybe i should have mentioned that
in my original post.

I am only looking for a very short explanation if possible!

Thanks again.
 
Reply With Quote
 
Pascal J. Bourguignon
Guest
Posts: n/a
 
      06-26-2008
Tarique <> writes:

> Can someone please explain the idea of Logical Constness as explained
> in section 10.2.7.1 Of Stroustrup's "The C++ Programming Language"
> ,with a short working code if possible.


Assume you have an object that has a very big vector of integers, and
that provides a method to get the sum the integers in this very
vector. Since computing this sum doesn't change the vector nor the
sum, it's a method that is logicall const. But if you try to declare
it formally const, then you cannot do the obvious optimization of
caching this sum (or you need to declare mutable the sum and the flag
that indicate that it's up-to-date).


class Strange {
protected:
std::vector<float> v(1000000);
public:
float getSum(void) const;
void set(int index,float value);
};

void Strange::set(int index,float value){
v[index]=value;
}

float Strange::getSum(void) const {
struct Summer{
float sum;
Summer():sum(0.0){}
void sum(float incr){ sum+=incr; }
};
Summer s();
for_each(v.begin(),v.end(),s); // very slow
return(s.sum);
}


So instead, we could lose the const, and while the method getSum is
still logically const, it can modify the object:


class Strange {
protected:
std::vector<float> v(1000000);
bool changed;
float sumCache;
public:
Strange():changed(true){}
float getSum(void) /* logically const */;
void set(int index,float value);
};

void Strange::set(int index,float value){
v[index]=value;
changed=true;
}

float Strange::getSum(void) /* logically const */ {
if(changed){
struct Summer{
float sum;
Summer():sum(0.0){}
void sum(float incr){ sum+=incr; }
};
Summer s();
for_each(v.begin(),v.end(),s); // very slow
sumCache=s.sum;
}
return(sumCache);
}



Another example:

For a class of rationals, the normalization method doesn't change the
value of the rationnal (4/8 == 1/2) so it is "logically const", while
it still changes the numerator and denominator.

--
__Pascal Bourguignon__
 
Reply With Quote
 
Gerhard Fiedler
Guest
Posts: n/a
 
      06-26-2008
On 2008-06-26 08:25:33, Tarique wrote:

> SeanW wrote:
>> On Jun 25, 2:40 pm, Tarique <peo_...@yahoo.com> wrote:
>>> Can someone please explain the idea of Logical Constness as explained in
>>> section 10.2.7.1 Of Stroustrup's "The C++ Programming Language" ,with a
>>> short working code if possible.
>>>
>>> Thank You

>>
>> This article covers it: http://www.ddj.com/cpp/184403892
>>
>> Sean

>
> I found this link when i Google'd it and i could not make much sense of
> it! Phew!!! I am only a C++ newbie. Maybe i should have mentioned that
> in my original post.
>
> I am only looking for a very short explanation if possible!


Logical constness means that an object appears as constant and works as if
it were constant, but in reality some of the internal states do change.

Gerhard
 
Reply With Quote
 
Tarique
Guest
Posts: n/a
 
      06-26-2008
Pascal J. Bourguignon wrote:
> Tarique <> writes:
>
>> Can someone please explain the idea of Logical Constness as explained
>> in section 10.2.7.1 Of Stroustrup's "The C++ Programming Language"
>> ,with a short working code if possible.

>
> Assume you have an object that has a very big vector of integers, and
> that provides a method to get the sum the integers in this very
> vector. Since computing this sum doesn't change the vector nor the
> sum, it's a method that is logicall const. But if you try to declare
> it formally const, then you cannot do the obvious optimization of
> caching this sum (or you need to declare mutable the sum and the flag
> that indicate that it's up-to-date).
>


_snip_


>
> Another example:
>
> For a class of rationals, the normalization method doesn't change the
> value of the rationnal (4/8 == 1/2) so it is "logically const", while
> it still changes the numerator and denominator.


Thank You Sir.That really helped.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-27-2008
On Jun 26, 1:25 pm, Tarique <peo_...@yahoo.com> wrote:
> SeanW wrote:
> > On Jun 25, 2:40 pm, Tarique <peo_...@yahoo.com> wrote:
> >> Can someone please explain the idea of Logical Constness as
> >> explained in section 10.2.7.1 Of Stroustrup's "The C++
> >> Programming Language" ,with a short working code if
> >> possible.


> > This article covers it:http://www.ddj.com/cpp/184403892


> I found this link when i Google'd it and i could not make much
> sense of it! Phew!!! I am only a C++ newbie. Maybe i should
> have mentioned that in my original post.


> I am only looking for a very short explanation if possible!


Logical const is generally opposed to bitwise const. Bitwise
const is what the compiler implements (because it doesn't
understand program logic). Logical const is what you, the
programmer decide. (Back in the days before there was a
consensus for logical const, it was sometimes known as
Humpty-Dumpty const---see "Through the Looking Glass" for
details.)

Basically, with logical const, when you design a class, you
decide what its value consists of; a const function will never
modify that value. If that value includes additional objects,
which are included indirectly, then you will not modify those
objects through a const function, even if the compiler allows
it. And if that value doesn't include some immediate members
(the classical example is cached data), then you may modify
those members in a const function (either by declaring them
mutable, or by using const_cast). The important thing is that
after calling a const function, client code will see the object
with the same value as it had before the function, for whatever
definition of value you have decided on for the class.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-27-2008
On Jun 26, 2:18 pm, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> Tarique <peo_...@yahoo.com> writes:
> > Can someone please explain the idea of Logical Constness as explained
> > in section 10.2.7.1 Of Stroustrup's "The C++ Programming Language"
> > ,with a short working code if possible.


> Assume you have an object that has a very big vector of
> integers, and that provides a method to get the sum the
> integers in this very vector. Since computing this sum
> doesn't change the vector nor the sum, it's a method that is
> logicall const. But if you try to declare it formally const,
> then you cannot do the obvious optimization of caching this
> sum (or you need to declare mutable the sum and the flag that
> indicate that it's up-to-date).


Unless you declare the cached value mutable, or use const_cast.

The more frequent use of logical const is the reverse case:
suppose you implement the vector using new and delete (rather
than vector). Your class then contains an
int* v ;
As far as the compiler is concerned, you can modify elements in
the vector even if the function is const, since the pointer
remains a pointer to a non-const. If you implement logical
const, however, you will not do so; any function which modifies
elements in the vector will be non-const. (But you've chosen a
good example. Modified as above, it can be used to show both
sides of the issue.)

By the way: the reason your example couldn't show this second
aspect using std::vector is because std::vector implements
logical const---internally, it contains a T*, and the compiler
would allow something like: T& vector<T>:perator[]( size_t )
const. This example, in fact, brings out a third issue of
logical const: if logical const is used, a const function will
not "leak" non-const references or pointers to anything which is
part of its value. Thus, the const version of
vector<>:perator[] returns a T const&, and not a T&.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Tarique
Guest
Posts: n/a
 
      06-29-2008
Tarique wrote:
> Can someone please explain the idea of Logical Constness as explained in
> section 10.2.7.1 Of Stroustrup's "The C++ Programming Language" ,with a
> short working code if possible.
>
> Thank You
> From - Thu


I have tried implementing the example given in the same section (Naive
though and only to test the idea!)

#include<iostream>
#include<string>
using namespace std;

class Date {
int d,m,y;
bool cache_valid;
string cache;
void compute_cache_value();//fill cache
//..
public:
Date(int dd = 0,int mm = 0,int yy = 0); // Default constructor
static Date default_date;
~Date(){}; // Destructor

string string_rep()const;//string representation
Date& add_day (int n);
};

Date Date::default_date(03,01,2000);
Date:ate(int dd,int mm,int yy)
{
cache_valid = false;
d = dd ? dd : default_date.d;
m = mm ? mm : default_date.m;
y = yy ? yy : default_date.y;

}


void Date::compute_cache_value()
{
switch(d) //Test_Only
{
case 1:case 8:case 15:case 22:case 29:cache = "Monday"; break;
case 2:case 9:case 16:case 23:case 30:cache = "Tuesday"; break;
case 3:case 10:case 17:case 24:case 31:cache = Wednesday";break;
case 4:case 11:case 18:case 25:cache = "Thursday"; break;
case 5:case 12:case 19:case 26:cache = "Friday"; break;
case 6:case 13:case 20:case 27:cache = "Saturday"; break;
case 7:case 14:case 21:case 28:cache = "Sunday"; break;
}
}

string Date::string_rep() const
{
if(cache_valid == false) {
Date* th = const_cast<Date*>(this);//Cast away const
th->compute_cache_value();
th->cache_valid = true;
}
return cache;
}

/*
According to Stroustrups "The C++ Programming Language",
the const_cast operator is _not guaranteed to work_ when applied to an
object that was originally declared as const.
Why is it so ?
*/

Date& Date::add_day (int n) { //Without any bounds check!
d+=n; //I assume d remains < 31
cache_valid = false;
return *this;
}

int main()
{
Date d1;
const Date d2;
string s1=d1.string_rep();
cout<<s1<<endl;

d1.add_day(2);
cout<<d1.string_rep()<<endl;

string s2=d2.string_rep(); //Undefined Behaviour .Why ?
cout<<s2<<endl;
return 0;
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Constness of the container of shared_ptr and the constness of it elements PengYu.UT@gmail.com C++ 14 04-04-2006 01:57 AM
reference to reference and constness problems with bind1st Marc C++ 6 07-06-2004 02:01 PM
Casting Away Constness Trevor Lango C++ 15 01-02-2004 10:27 PM
Constness with pointers to pointers etc. Richard Hayden C++ 1 11-23-2003 01:20 PM
Casting away constness Martin Magnusson C++ 1 11-17-2003 01:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57