Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Data Types

48 views
Skip to first unread message

Real Troll

unread,
Jan 16, 2018, 8:52:15 PM1/16/18
to
I wonder whether there is any need to define data types these days when
we can easily declare automatic types using auto? For example this
program compiles just fine in modern compilers:

/************************************ auto.cpp
************************************/
#include <iostream>
#include <typeinfo>
#include <stdlib.h>

int main()
{
std::cout << "\t [auto.cpp]" << std::endl;
// Creating several auto-type variables
auto a = 1;
auto b = 1.0;
auto c = a + b;
auto d = {b, c};
// Displaying the preceding variables' type
std::cout << "\t type of a: " << typeid(a).name() << std::endl;
std::cout << "\t type of b: " << typeid(b).name() << std::endl;
std::cout << "\t type of c: " << typeid(c).name() << std::endl;
std::cout << "\t type of d: " << typeid(d).name() << std::endl;
return EXIT_SUCCESS;
}

/************************************ End
************************************/


Lynn McGuire

unread,
Jan 16, 2018, 9:21:11 PM1/16/18
to
Auto will not work for function arguments.

std::vector <std::string> PFXGroup::controllableSpecs (int index, int key)

Lynn

Jorgen Grahn

unread,
Jan 17, 2018, 7:50:38 AM1/17/18
to
On Wed, 2018-01-17, Real Troll wrote:
> I wonder whether there is any need to define data types

You mean "spell out the type" or something.

> these days when
> we can easily declare automatic types using auto? For example this
> program compiles just fine in modern compilers:
...
> auto a = 1;
> auto b = 1.0;
> auto c = a + b;
> auto d = {b, c};
...

Don't forget 'const auto' and 'const auto&'.

I don't think there's any concensus about when and how often to use
'auto'. Some say "as much as possible!" which I disagree with.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Alf P. Steinbach

unread,
Jan 17, 2018, 8:21:46 AM1/17/18
to
On 1/17/2018 1:50 PM, Jorgen Grahn wrote:
> On Wed, 2018-01-17, Real Troll wrote:
>> I wonder whether there is any need to define data types
>
> You mean "spell out the type" or something.
>
>> these days when
>> we can easily declare automatic types using auto? For example this
>> program compiles just fine in modern compilers:
> ...
>> auto a = 1;
>> auto b = 1.0;
>> auto c = a + b;
>> auto d = {b, c};

What's that last?


> ...
>
> Don't forget 'const auto' and 'const auto&'.
>
> I don't think there's any concensus about when and how often to use
> 'auto'. Some say "as much as possible!" which I disagree with.

Me too, although the original Almost Always Auto author was Herb Sutter,
with review by Scott Meyers and Andrei Alexandrescu.


Cheers!,

- Alf

Alf P. Steinbach

unread,
Jan 17, 2018, 8:22:34 AM1/17/18
to
On 1/17/2018 2:21 PM, Alf P. Steinbach wrote:
> On 1/17/2018 1:50 PM, Jorgen Grahn wrote:
>> On Wed, 2018-01-17, Real Troll wrote:
>>> I wonder whether there is any need to define data types
>>
>> You mean "spell out the type" or something.
>>
>>> these days when
>>> we can easily declare automatic types using auto?  For example this
>>> program compiles just fine in modern compilers:
>> ...
>>>       auto a = 1;
>>>       auto b = 1.0;
>>>       auto c = a + b;
>>>       auto d = {b, c};
>
> What's that last?

Oh, OK, it's a `std::initizer_list`. Moral: don't post when you feel the
urge to go the bathroom.

Chris Vine

unread,
Jan 17, 2018, 4:11:04 PM1/17/18
to
On Tue, 16 Jan 2018 20:21:01 -0600
Lynn McGuire <lynnmc...@gmail.com> wrote:
> Auto will not work for function arguments.

Unless its a lambda expression.

Lynn McGuire

unread,
Jan 17, 2018, 10:29:20 PM1/17/18
to
And you lost me.

Lynn


Alf P. Steinbach

unread,
Jan 18, 2018, 1:25:54 AM1/18/18
to
With a lambda you can write e.g.

auto const foo = []( auto& x ) { cout << x << endl; };
foo( "The answer is... " );
foo( 0b101010 );

It's roughly equivalent to this C++03 code:

struct Lambda_478
{
template< class Type >
void operator()( Type& x ) const { cout << x << endl; }
};

Lambda_478 const foo;
foo( "The answer is... " );
foo( 0b101010 );


Cheers & hth.,

- Alf

Alf P. Steinbach

unread,
Jan 18, 2018, 1:27:42 AM1/18/18
to
Oh wait. C++03 didn't have binary literals. Oops.

Cheers!,

- Alf


Vir Campestris

unread,
Jan 18, 2018, 3:56:30 PM1/18/18
to
On 17/01/2018 12:50, Jorgen Grahn wrote:
> I don't think there's any concensus about when and how often to use
> 'auto'. Some say "as much as possible!" which I disagree with.

I say "As little as possible".

But, hey, that's what coding standards are for. I'm trying to get some
introduced here...

Andy
0 new messages