473,396 Members | 1,797 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,396 software developers and data experts.

when a const isn't? (typedef?)

How come this behaves the way it does? val_D() and val_E() cause a
(correct) compiler error. val_F() and val_G() do not, but I don't see
why.

Can anyone enlighten me?
#include <iostream>
using namespace std;

class foo {
public:
typedef int value_type;
typedef value_type& reference;
typedef const reference const_reference;

explicit foo(int x): m_val(x) {}

int val() { return m_val; }

int& val_A() { return m_val; }
value_type& val_B() { return m_val; }
reference val_C() { return m_val; }

const int& val_D() { return m_val; }
const value_type& val_E() { return m_val; }
const reference val_F() { return m_val; }
const_reference val_G() { return m_val; }

private:
int m_val;
};

int main()
{
foo x(0);

cout << x.val() << endl;
x.val_A() = 1;
x.val_B() = 2;
x.val_C() = 3;
x.val_D() = 4; // compile error
x.val_E() = 5; // compile error
x.val_F() = 6;
x.val_G() = 7;
cout << x.val() << endl;

return 0;
}

Mar 19 '07 #1
1 1250
On 19 Mar, 03:25, "Tim H" <thoc...@gmail.comwrote:
How come this behaves the way it does?
<...>

At this point below you are attempting to apply constness to a
reference which has no effect. constness can only affect actual
objects if you get my meaning...
typedef const reference const_reference;
VC7.1 gives:

d:\Projects\Test\Test.cpp(10) : warning C4181: qualifier applied to
reference type; ignored

Similarly when used in the function return later...

The generic way to approach adding const to references is to use
typetraits:
(boost or std::tr1)

First remove any refernce, then add const then ree-add reference
where T is some type
typedef typename add_reference<
typename add_const< typename remove_reference<T>::type>::type
>::type const_reference;
(not tested)

regards
Andy Little

Mar 19 '07 #2

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

Similar topics

2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
10
by: Tony Johansson | last post by:
Hello Experts!! This class template and main works perfectly fine. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter in...
27
by: Sundar | last post by:
Do we have something called a const function ? I thought applying the 'const' specifier to a function is meaningless... but when i tried defining a const function it worked !! like... const int...
11
by: x-pander | last post by:
given the code: <file: c.c> typedef int quad_t; void w0(int *r, const quad_t *p) { *r = (*p); }
9
by: vashwath | last post by:
Hi all, Recently I attended an interview in which the question "Is there any difference between "const T var" and "T const var"? was asked.I answered "NO"(I guessed it:-( ).Did I answered...
4
by: QQ | last post by:
Hello I am a newbie on network programming. I am trying to receive a packet if((numbytes = recvfrom(udp_fd1, buf, MAXLEN-1, 0,(struct sockaddr*)&register_addr, &addr_len))==-1){ fprintf(stderr,...
14
by: Tim H | last post by:
I understand the semantics of why this works the way it does. But I wonder if there's a reason for the behaviore at the line marked "QUESTION". I figured if there is an answer, someone here knows...
15
by: Jess | last post by:
Hello, Sometimes declarations are all what we need when we define/declare classes (or functions?), but sometimes we need definitions. I learned that if we define a class (B) that has an object...
9
by: t | last post by:
Can you use a plain iterator to vector<const intto write to it? It doesn't seem like it should be possible, but Visual C++ 2005 Express lets me: vector<const intvc; vc.push_back(5);...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.