Is there a "constify" function/macro?

  • Follow


Hello,
Is there some way to make a variable const after the fact? For
example, say there was a function called constify, I could use it in
situations like:

  string name;
  cout << "Enter your name:";
  cin >> name;
  constify(name);

or..

  for(int i = 0 ; i < 10 ; i ++)
  {
    constify(i)


I understand that I can achieve the same thing with multiple
variables, like:

  string mut_name;
  cout << "Enter your name:";
  cin >> mut_name;
  const string& name(mut_name); // name is const now.

However that's a bit of excess typing, which itself would be prone to
errors. It would be nice is there was a terse macro to achieve the
same thing.

Or, if a "constify" function is not possible, is there a way to make a
"deconstify" function? Then I could write code like:

  const string name;
  cout << "Enter your name:";
  cin >> deconstify(name);

I 'de-consting' a variable with a const_cast but it didn't seem to
work properly. It worked for string, but not for int (using g++ (GCC)
4.3.2, code is below.)

Thanks,
Derek.
====code====

#include <iostream>

using namespace std;

int main()
{
	const int x(111);
	cout << "Enter x:" << endl;
	cin >> const_cast<int&>(x) ;
	cout << "x is " << x << endl;
}

===output===

../a.out
Enter x:
123
x is 111

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply derek352 (2) 9/29/2009 6:51:37 PM

On Sep 30, 2:51 am, Derek <de...@antiquark.com> wrote:
> Hello,
> Is there some way to make a variable const after the fact? For
> example, say there was a function called constify, I could use it in
> situations like:
>
>   string name;
>   cout << "Enter your name:";
>   cin >> name;
>   constify(name);

This specific idea cannot be done because of things like:
if (user_input_box_returns_yes("Should I Constify?")) constify(name)

It would force a run-time check for constness.

Maybe there is some more limited static compilation-time way of doing
this, but I don't know of any.


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply tohava 9/29/2009 10:45:09 PM


On 30 Sep, 01:51, Derek <de...@antiquark.com> wrote:
> Hello,
> Is there some way to make a variable const after the fact? For
> example, say there was a function called constify, I could use it in
> situations like:
>
>   string name;
>   cout << "Enter your name:";
>   cin >> name;
>   constify(name);

I think that you are trying to solve a problem that doesn't exist.

I can what you mean but my thinking would be that if the function is
short then you are not likely to accidentally modify name and if it is
so long that you forget what name is supposed to be then it should be
broken into sub-functions taking const string&.

Even if there were such a thing I don't see how it could be any
shorter than your const reference suggestion (which the compiler
should optimize anyway).





-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Nick 9/30/2009 4:35:21 AM

Derek wrote:
> Hello,
> Is there some way to make a variable const after the fact?

No, there isn't. C++ does almost all of its type checking at compile
time - types and constness gets fixed then.

[snip]

> Or, if a "constify" function is not possible, is there a way to make a
> "deconstify" function? Then I could write code like:
> 
>   const string name;
>   cout << "Enter your name:";
>   cin >> deconstify(name);
> 
> I 'de-consting' a variable with a const_cast but it didn't seem to
> work properly. It worked for string, but not for int (using g++ (GCC)
> 4.3.2, code is below.)
> 
> Thanks,
> Derek.
> ====code====
> 
> #include <iostream>
> 
> using namespace std;
> 
> int main()
> {
> 	const int x(111);
> 	cout << "Enter x:" << endl;
> 	cin >> const_cast<int&>(x) ;
> 	cout << "x is " << x << endl;
> }
> 
> ===output===
> 
> ./a.out
> Enter x:
> 123
> x is 111

Casts of any sort always produce a new value - they never change the
thing the cast is applied to.

Neil Butterworth

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Neil 9/30/2009 4:56:32 AM

Derek schrieb:

> Is there some way to make a variable const after the fact? 

no

>   string name;
>   cout << "Enter your name:";
>   cin >> name;
>   constify(name);

but you can structur the code so, that name is declared as a constant or
after initialization, call a function where the passed parameter is a
constant or a reference to constant.

const string name = inquire_name(std::in, std::cout);

or

string name;
cout << "Enter your name:";
cin >> name;

process(name);

best regards

Torsten

-- 
kostenlose Wirtschaftssimulation: http://www.financial-rumors.de

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Torsten 9/30/2009 5:56:04 AM

On Wed, 30 Sep 2009 06:21:37 +0530, Derek <derek@antiquark.com> wrote:

> Hello,
> Is there some way to make a variable const after the fact? For
> example, say there was a function called constify, I could use it in
> situations like:
>
>   string name;
>   cout << "Enter your name:";
>   cin >> name;
>   constify(name);
>
> or..
>
>   for(int i = 0 ; i < 10 ; i ++)
>   {
>     constify(i)
>
>
> I understand that I can achieve the same thing with multiple
> variables, like:
>
>   string mut_name;
>   cout << "Enter your name:";
>   cin >> mut_name;
>   const string& name(mut_name); // name is const now.
>
> However that's a bit of excess typing, which itself would be prone to
> errors. It would be nice is there was a terse macro to achieve the

Then const'ness will have to be checked at runtime. And in case on
a system constants goto some ROM kind of memory it is yet another
trouble.

Why do you need it?

I find your extra typing thing good/sufficient enough if you really want
it.


Regards,
Jyoti

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply jyoti 9/30/2009 6:09:15 AM

Derek wrote:
> Hello,
> Is there some way to make a variable const after the fact? For
> example, say there was a function called constify, I could use it in
> situations like:
> 
>   string name;
>   cout << "Enter your name:";
>   cin >> name;
>   constify(name);

There is no way to do this in the sense of actually changing the
declared type of a variable at run time so that all subsequent uses of
of your variable 'name' treat it as a const, if that is what you are
asking. There may, however, be use cases in which a form of 'constify'
function might be applied to control how a variable is used. For
instance, the following could be used to assist in overload
resolution, depending upon whether in any one context you require your
variable to be treated as const or not.

    template<typename T>
    const T& constify(T& t)
    {
       return t;
    }

    void f(int& i)				// #1
    {
       // ... can have side-effects on i
    }

    void f(const int& i)				// #2
    {
       // ... i is constant
    }

    int main()
    {
       int i;
       f(i);				  // calls #1
       f(constify(i));			  // calls #2
    }

> I 'de-consting' a variable with a const_cast but it didn't seem to
> work properly. It worked for string, but not for int (using g++ (GCC)
> 4.3.2, code is below.)
<snip></snip>
> ====code====
> 
> #include <iostream>
> 
> using namespace std;
> 
> int main()
> {
> 	const int x(111);
> 	cout << "Enter x:" << endl;
> 	cin >> const_cast<int&>(x) ;
> 	cout << "x is " << x << endl;
> }

In 5.2.11 [expr.const.cast] par. 7 of the current standard:

"[Note: Depending on the type of the object, a write operation through a
pointer, lvalue or pointer to data member resulting from a const_cast that casts
away a const-qualifier may produce undefined behaviour (7.1.5.1).]"

and:

7.1.5.1 [dcl.type.cv] par. 4:

"Except that any class member declared mutable can be modified, any attempt to
modify a const object during its lifetime results in undefined behaviour."

where the example given includes:

    const int *ciq = new const int(3);
    int *iq = const_cast<int *>(ciq);
    *iq = 4;	// undefined: modifies a const object

I believe that this - undefined behaviour - is what you are experiencing in that
your attempts appear to produce the results you expect for string and not for
int. But of course, undefined behaviour may indeed match your expectations ...
on this occasion, on this or that system, on a Tuesday when the moon is full!

Regards

Paul Bibbings


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Paul 9/30/2009 6:10:09 AM

"Nick Hounsome" <nick.hounsome@googlemail.com>

> On 30 Sep, 01:51, Derek <de...@antiquark.com> wrote:
>> Hello,
>> Is there some way to make a variable const after the fact? For
>> example, say there was a function called constify, I could use it in
>> situations like:
>>
>>   string name;
>>   cout << "Enter your name:";
>>   cin >> name;
>>   constify(name);
>
> I think that you are trying to solve a problem that doesn't exist.

The problem exists very well. And having a feature that add constness would
make coding saver and better.

For the OP: no, the language currently has nothing of a kind.  Some 5 years
ago I was thinking to write a proposal, but tal on csc++ made it clear it
would have 0 chance. (you may look up discussion searching 'finalize' in
subject; though be aware that Google's group search is broken for several
months now.)

Though the next standard will have some tools that allow better or easier
workarounds, the auto keyword makes simpler to create a const & of an
existing element, lambdas may possibly used to do initiazlistaion and have
const objects right there.

Though the simplest cases, like  that for loop var is still left to pure
discipline. :-(

> I can what you mean but my thinking would be that if the function is
> short then you are not likely to accidentally modify name

On that thinking we could drop most language elements including const in
general, scope, the type system, etc.    Back when C was new it compiled
almost anything, why it morphed to mandatory function declarations, dropping
implicit int, in C++ stricter conversion, ....?

I like language where I can express the most about the state and the flow --
and expect the compiler find most breaches at compile time.  const is king
in this regard, too bad it can't be expressed/used in some situations.

> and if it is
> so long that you forget what name is supposed to be then it should be
> broken into sub-functions taking const string&.

Breaking stuff to function based on length instead of based DRY and/or
abstraction is a common source of problems.

> Even if there were such a thing I don't see how it could be any
> shorter than your const reference suggestion (which the compiler
> should optimize anyway).

It need not be shorter. Having const& still leaves the original name around,
so it does not have the same evvect as a constify/finalize would.




-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Balog 9/30/2009 2:44:54 PM

Thanks all for your responses, and alternate ways of solving the
problem.

I actually rigged up a CONSTIFY macro that sort of does what I want.
However it needs a matching END_CONSTIFY macro at the end of the
block, and it uses the g++ extension "typeof". Is there some way to
eliminate the END_CONSTIFY macro in the code below (compiled with g++
4.3.2)?

Also, does this macro appear to have any egregious flaws that could
cause big problems?

Thanks
Derek Ross.

======CODE======

#include <iostream>

using namespace std;

#define CONSTIFY(x) { const typeof(x) & _cfy_##x (x) ; { const typeof
(x) & x (_cfy_##x);

#define END_CONSTIFY  }}

int main()
{
	int x(111);
	string s("none");
	cout << "Enter x:";
	cin >> x;
	cout << "Enter s:";
	cin >> s;

	CONSTIFY(x);
	CONSTIFY(s);

	cout << "x is " << x << endl;
	x ++; // compile error.

	cout << "s is " << s << endl;
	s += "."; // compile error

	END_CONSTIFY;
	END_CONSTIFY;
}

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Derek 9/30/2009 2:49:04 PM

On Sep 30, 2:51 am, Derek <de...@antiquark.com> wrote:
> Hello,
> Is there some way to make a variable const after the fact? For
> example, say there was a function called constify, I could use it in
> situations like:
>
>   string name;
>   cout << "Enter your name:";
>   cin >> name;
>   constify(name);
>
> or..
>
>   for(int i = 0 ; i < 10 ; i ++)
>   {
>     constify(i)
>
> I understand that I can achieve the same thing with multiple
> variables, like:
>
>   string mut_name;
>   cout << "Enter your name:";
>   cin >> mut_name;
>   const string& name(mut_name); // name is const now.
>
> However that's a bit of excess typing, which itself would be prone to
> errors. It would be nice is there was a terse macro to achieve the
> same thing.
As a matter of fact, I think the exact opposite; C++ wants users
(programmers) to be explicit about types and conversions. Otherwise,
it's much more difficult to deduce types of identifiers. const-ness is
enforced at compile time, not at run-time. Much like you have used
const_cast<T> (although by causing a UB), programmer has to explicitly
add or remove const qualifier. const-ness can't change with a function
call; one needs a const (or non-const - depending on what is expected)
qualified type and use it, or explicitly cast it to a non-const type.
Casting _to_ const qualified type is implicit but otherwise needs
explicit casting.

T* p;
const T* const_p;

const_pointer = p; // OK
p = const_p; // error

Therefore, you can pass "name" to a function that accepts a const
std::string& and conversion will be implicit (no casting needed) but
you can't do it at run-time.

>
> Or, if a "constify" function is not possible, is there a way to make a
> "deconstify" function? Then I could write code like:
>
>   const string name;
>   cout << "Enter your name:";
>   cin >> deconstify(name);
>

No. If you want to mutate the object, then why is it const? If you do
not want to mutate the object and use const qualified type, then don't
mutate.

You need to be explicit about these.

void get_name(std::string& refString)
{
   std::cin >> refString;
}

void use_name(const std::string& refString)
{
}

As you know, you won't be able to call non-const qualified methods of
std::string inside use_name function.

Being explicit about conversions is essential in C++.

> I 'de-consting' a variable with a const_cast but it didn't seem to
> work properly. It worked for string, but not for int (using g++ (GCC)
> 4.3.2, code is below.)
>
> Thanks,
> Derek.
> ====code====
>
> #include <iostream>
>
> using namespace std;
>
> int main()
> {
>         const int x(111);
>         cout << "Enter x:" << endl;
>         cin >> const_cast<int&>(x) ;
>         cout << "x is " << x << endl;
>
> }
>
> ===output===
>
> ./a.out
> Enter x:
> 123
> x is 111
>

That's undefined behavior.


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Ismail 9/30/2009 2:49:21 PM

On 30/09/09 01:51, Derek wrote:
> Hello,
> Is there some way to make a variable const after the fact? For
> example, say there was a function called constify, I could use it in
> situations like:
>
>    string name;
>    cout<<  "Enter your name:";
>    cin>>  name;
>    constify(name);
>
> or..
>
>    for(int i = 0 ; i<  10 ; i ++)
>    {
>      constify(i)
>
>
> I understand that I can achieve the same thing with multiple
> variables, like:
>
>    string mut_name;
>    cout<<  "Enter your name:";
>    cin>>  mut_name;
>    const string&  name(mut_name); // name is const now.
>
> However that's a bit of excess typing, which itself would be prone to
> errors. It would be nice is there was a terse macro to achieve the
> same thing.
>
> Or, if a "constify" function is not possible, is there a way to make a
> "deconstify" function? Then I could write code like:
>
>    const string name;
>    cout<<  "Enter your name:";
>    cin>>  deconstify(name);
>
> I 'de-consting' a variable with a const_cast but it didn't seem to
> work properly. It worked for string, but not for int (using g++ (GCC)
> 4.3.2, code is below.)
>
> Thanks,
> Derek.
> ====code====
>
> #include<iostream>
>
> using namespace std;
>
> int main()
> {
> 	const int x(111);
> 	cout<<  "Enter x:"<<  endl;
> 	cin>>  const_cast<int&>(x) ;
> 	cout<<  "x is "<<  x<<  endl;
> }
>
> ===output===
>
> ./a.out
> Enter x:
> 123
> x is 111
>

Surely if you don't wish a function parameter to be modified during some
operation then pass it as a const reference/pointer, or declare a member
function as a const function to avoid any of it's data members being
altered.

A pointless exercise, but you could easily create a template function
something like this

template <typename T>
const T &makeConst(T &inp)
{
	return inp;
}

JB

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply cpp4ever 9/30/2009 6:26:33 PM

* tohava:
> On Sep 30, 2:51 am, Derek <de...@antiquark.com> wrote:
>> Hello,
>> Is there some way to make a variable const after the fact? For
>> example, say there was a function called constify, I could use it in
>> situations like:
>>
>>   string name;
>>   cout << "Enter your name:";
>>   cin >> name;
>>   constify(name);
> 
> This specific idea cannot be done because of things like:
> if (user_input_box_returns_yes("Should I Constify?")) constify(name)
> 
> It would force a run-time check for constness.

Uh, well.

<code>
#include <iostream>
#include <string>

using namespace std;

int main()
{
     string name;

     cout << "Enter your name, please: ";
     getline( cin, name );
     {
         string const& constName = name;
         string const& name      = constName;

         cout << "It seems your name is " << name << "!" << endl;
     }
}
</code>


> Maybe there is some more limited static compilation-time way of doing
> this, but I don't know of any.

A better way than the above is to define a suitable function-like routine,


<code comment="Eror handing omited">
#include <iostream>
#include <string>

using namespace std;

string lineInput( string const& prompt )
{
     string line;
     cout << prompt;
     getline( cin, line );
     return line;
}

int main()
{
     string const name   = lineInput( "Enter your name, please: " );

     cout << "It seems your name is " << name << "!" << endl;
}
</code>


Cheers & hth.,

- Alf

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Alf 10/1/2009 2:10:24 AM

> Breaking stuff to function based on length instead of based DRY and/or
> abstraction is a common source of problems.

I agree that you can't just break it down by numbers but I've yet to
come across a large function that couldn't be broken down logically
into sensible sub functions of reasonable size.

> > Even if there were such a thing I don't see how it could be any
> > shorter than your const reference suggestion (which the compiler
> > should optimize anyway).
>
> It need not be shorter. Having const& still leaves the original name around,
> so it does not have the same evvect as a constify/finalize would.

In all my years programming I don't remember ever finding this to be a
problem.

Most C++ programmers are maintaining not designing and most
maintainance programmers are not particularly keen or particularly
good.
It is my experience that people maintaining code (not me) will move a
needed declaration to the top of a function rather than find the
minimum required scope. I would therefore expect them to do away with
the constifier the moment the compiler complains.

Even now I frequently find people with years of C/C++ experience who
simply don't use const! Faced with a compiler complaint about
constness their first thought is to remove the const. OK I know that
the answer is to have them shot but it demonstrates how  little
enthusiasm I expect there to be for this idea.



-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Nick 10/1/2009 4:38:44 PM

Balog Pal wrote:
> (you may look up discussion searching 'finalize' in
> subject; though be aware that Google's group search is broken for several
> months now.)

Thanks, I found the old thread, lots of interesting discussion and
ideas there.

You might be interested in another macro I wrote, LOCALCONST (see
below).  It's not as powerful as constify/finalize, as it will only
work within a sub-block. However it lets you constify variables within
a for loop, which, in itself, is a very useful feature.

Cheers,
Derek.

=== CODE === (compiled with g++ GCC 4.3.2)

#include <iostream>
using namespace std;

#define LOCALCONST(x) const typeof(x) & cnstfy_vrbl__##x (x) ;  const
typeof(x) & x (cnstfy_vrbl__##x) ;

string the_global = "444";

int main()
{
	LOCALCONST(the_global);

	cout << "the_global:" << the_global << endl;

	for(int i = 0 ; i < 3 ; i ++)
	{
		LOCALCONST(i);
		for(int j = 0 ; j < 3 ; j ++ )
		{
			LOCALCONST(j);
			cout << i << " " << j << endl;
		}
	}

	int x(111);
	string y("222");
	int z(333);

	{
		cout << "Enter x:";
		cin >> x;
		LOCALCONST(x);

		cout << "Enter y:";
		cin >> y;
		LOCALCONST(y);

		cout << "Enter z:";
		cin >> z;
		LOCALCONST(z);

		cout << "x is " << x << endl;
		cout << "y is " << y << endl;
		cout << "z is " << z << endl;
	}
}


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Derek 10/1/2009 4:39:45 PM

Derek wrote:

> Thanks all for your responses, and alternate ways of solving the
> problem.
> 
> I actually rigged up a CONSTIFY macro that sort of does what I want.
> However it needs a matching END_CONSTIFY macro at the end of the
> block, and it uses the g++ extension "typeof". Is there some way to
> eliminate the END_CONSTIFY macro in the code below (compiled with g++
> 4.3.2)?

You could leave the creation of a new block up to the user.
Then your CONSTIFY macro would read:
#define CONSTIFY(x) const typeof(x)& _cfy_##x (x); const typeof(x)& \
  x(_cfy_##x);

> 
> Also, does this macro appear to have any egregious flaws that could
> cause big problems?

Apart from the fact that it re-declares an existing identifier (which is
often forbidden in coding guidelines and warned about by compilers), no.

> 
> Thanks
> Derek Ross.
> 
Bart v Ingen Schenau
-- 
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://c-faq.com/
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Bart 10/1/2009 4:41:08 PM

> > Hello,
> > Is there some way to make a variable const after the fact? For
> > example, say there was a function called constify, I could use it in
> > situations like:
>
> >   string name;
> >   cout << "Enter your name:";
> >   cin >> name;
> >   constify(name);
>
> This specific idea cannot be done because of things like:
> if (user_input_box_returns_yes("Should I Constify?")) constify(name)
>
> It would force a run-time check for constness.
>


What if we force "constify" to appear in the same scope of the
variable it is applied to?

Another idea:

    string const name {  // "name" mutable only inside this block
         cout << "Enter your name:";
         cin >> name;
    }


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply ManuelPeinado 10/2/2009 5:58:13 AM

ManuelPeinado wrote:
> Another idea:
> 
>     string const name {  // "name" mutable only inside this block
>          cout << "Enter your name:";
>          cin >> name;
>     }

We already can achieve the desired effect with current C++:

struct read_from
{
   std::istream& input;
   read_from(std::istream& in) : input(in) {}

   template<typename T> operator T() const
   {
     T result;
     input >> result;
     return result;
   }
};

....
   std::cout << "Enter your name: ";
   std::string const s = read_from(std::cin);

   std::cout << "Enter your age: ";
   int const i = read_from(std::cin);


Falk

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply ISO 10/2/2009 11:43:49 AM

ManuelPeinado wrote:
>>> Hello,
>>> Is there some way to make a variable const after the fact? For
>>> example, say there was a function called constify, I could use it in
>>> situations like:
>>>   string name;
>>>   cout << "Enter your name:";
>>>   cin >> name;
>>>   constify(name);
>> This specific idea cannot be done because of things like:
>> if (user_input_box_returns_yes("Should I Constify?")) constify(name)
>>
>> It would force a run-time check for constness.
>>
> 
> 
> What if we force "constify" to appear in the same scope of the
> variable it is applied to?
> 
> Another idea:
> 
>     string const name {  // "name" mutable only inside this block
>          cout << "Enter your name:";
>          cin >> name;
>     }
> 
But you can already effectively achieve that by using a function:

std::string getname(){
    std::string name;
    std::cout << "Please Enter your name" << std::endl;
    std::getline(std::cin, name);
    return name;
}

string const name{getname()}; // using C++0x {} initialiser syntax

With the advent of move semantics in C++0x you won't even pay much for
the string copying. I am not sure whether the grammar allows use of a
lambda function in an intialiser but if it does the initialising
function can be local and unnamed.



-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Francis 10/2/2009 11:45:33 AM

17 Replies
89 Views

(page loaded in 0.146 seconds)


Reply: