473,473 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

isConst - Loki

I copied this from several sources online. Why doesn't it work?

// ================================================== ==============
#include <iostream>

using namespace std;

namespace utl
{
template <typename T>
class TypeTraits
{
private:
template<class U>
struct UnConst
{
typedef U Result;
enum { isConst = false };
};

template<class U>
struct UnConst<const U>
{
typedef U Result;
enum { isConst = true };
};

public:

enum { isConst = UnConst<T>::isConst };
};
}

class Object
{
public:
Object(int a) { i = a; }
int i;
};

int main()
{
Object test(10);
const Object* t = &test;

if ( utl::TypeTraits<const Object*>::isConst )
cout << "Hooray - it's a const :)\n";
else
cout << "It's NOT a const :(\n";

if ( utl::TypeTraits<Object*>::isConst )
cout << "it's a const :(\n";
else
cout << "Hooray - It's NOT a const :)\n";

cout << endl;
}

// ================================================== ==============
Here is the output:

It's NOT a const :(
Hooray - It's NOT a const :)
Jul 19 '05 #1
4 1835
skscpp wrote:
I copied this from several sources online. Why doesn't it work?
...
int main()
{
Object test(10);
const Object* t = &test;

if ( utl::TypeTraits<const Object*>::isConst )
cout << "Hooray - it's a const :)\n";
else
cout << "It's NOT a const :(\n";

if ( utl::TypeTraits<Object*>::isConst )
cout << "it's a const :(\n";
else
cout << "Hooray - It's NOT a const :)\n";

cout << endl;
}
...
Here is the output:

It's NOT a const :(
Hooray - It's NOT a const :)


"Doesn't work"? Where? I don't seen any problems with it. Neither 'const
Object*' nor 'Object*' is 'const' type so the output should be 'It's NOT
a const' in both cases. That's exactly what you got.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #2

"Alf P. Steinbach" <al***@start.no> wrote in message
news:3f****************@News.CIS.DFN.DE...
If you consistently refrain from placing 'const' in front of the
type name, which is a special case syntax, then you'll probably not
get confused about this again.


How is that a special case syntax? In other words, what is that special
case?

So I should always use:
Object const * (pointer to constant object)
OR
Object * const (const pointer to object)


Jul 19 '05 #3
sk*****@hotmail.com (skscpp) wrote in message news:<3a**************************@posting.google. com>...
I copied this from several sources online. Why doesn't it work?

// ================================================== ==============
#include <iostream>

using namespace std;

namespace utl
{
template <typename T>
class TypeTraits
{
private:
template<class U>
struct UnConst
{
typedef U Result;
enum { isConst = false };
};

template<class U>
struct UnConst<const U>
{
typedef U Result;
enum { isConst = true };
};

public:

enum { isConst = UnConst<T>::isConst };
};
}

class Object
{
public:
Object(int a) { i = a; }
int i;
};

int main()
{
Object test(10);
const Object* t = &test;

if ( utl::TypeTraits<const Object*>::isConst )
cout << "Hooray - it's a const :)\n";
else
cout << "It's NOT a const :(\n";

if ( utl::TypeTraits<Object*>::isConst )
cout << "it's a const :(\n";
else
cout << "Hooray - It's NOT a const :)\n";

cout << endl;
}

// ================================================== ==============
Here is the output:

It's NOT a const :(
Hooray - It's NOT a const :)


Try the following, you'll get a const!

if ( utl::TypeTraits<Object * const>::isConst )
cout << "Hooray - it's a const :)\n";
else
cout << "It's NOT a const :(\n";
Jul 19 '05 #4
"Victor Bazarov" <v.********@attAbi.com> wrote in message news:<xJGTa.131566$Ph3.17100@sccrnsc04>...
Special case is allowing 'const' before type:

const int a;

is allowed along with more proper

int const a;
"Proper" makes it sound like the latter is objectively better than the
former. Where to put "const" is just as much a style issue as where to
put curly braces, the most significant byte, the big end of the egg,
etc.

I still look at "int const a;" funny. There's nothing inherently wrong
with it, I understand what it means, and I know that "right-constians"
do it that way in an attempt to mimic the way C++ seems to interpret
it (right-to-left). However, it's not my style, and it wouldn't
improve my ability to read or write C++ or the compiler's ability to
interpret and translate it.

Beyond understanding the difference between "before *" and "after *",
it really doesn't matter what order specifiers come in. Really. :)
That's one of the causes of confusion among those who haven't
got used to reading declarations.


I think the confusion is almost entirely the result of C declarator
syntax, described by Stroustrup as an "experiment that failed." Since
we're stuck with what we've got, and a *lot* of code which takes
advantage of what we've got, the only viable solution is to get used
to reading it.

- Shane
Jul 19 '05 #5

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

Similar topics

1
by: sks_cpp | last post by:
How can I get the following to work? Are the templates defined incorrectly? I am running this on Visual C++ 6.0. // =============================================== #include <iostream> using...
0
by: Sebastian Faust | last post by:
Hi, I read about the Loki::Factory and Loki::AbstractFactory in the book Modern C++ Design and couldnt figure out one thing: If I have several classes, for example the following hierarchie: ...
1
by: Dave | last post by:
Is anybody aware of a newsgroup devoted to Loki? I assume this forum would not be the most appropriate place to discuss a specific library, even if that library is written purely in standard...
3
by: Krivenok Dmitry | last post by:
I writing simple class CmdLine: ..... ..... class CmdLine { ..... ..... public: /// Constructor CmdLine(int argc, char** argv);
3
by: Krivenok Dmitry | last post by:
Hello All! This is example of code: ////////////////////////////////////////////////////////////////////////////////////////////////// #ifndef A_H_ #define A_H_ #include <Loki/Singleton.h>...
2
by: Martin Herbert Dietze | last post by:
Hi, in a project I am using callbacks which are called like ordinary functions but in fact are Loki::Functor's encapsulating calls to non-static member functions on instances of different...
10
by: Ray | last post by:
I am reading Andrei Alexandrescu's book. The ideas presented there sound really good, but I wonder--is there really a lot of people using it? Or it's simply too esoteric for mortals? Cheers Ray
2
by: Shawn McGrath | last post by:
Hey, I can't get DeletableSingleton to actually delete the singleton. The code is: typedef Loki::SingletonHolder<GenClass Loki::CreateUsingNew, Loki::DeletableSingletonGen; .......
3
by: aaragon | last post by:
Hello everyone, I've been trying to work with the visitor design pattern, and it works fine except for the following. Let's suppose that we have a fixed hierarchy of classes (many of them)...
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.