is 'auto' in c++ similar to 'Object' in java? [duplicate] - c++

This question already has answers here:
C++ auto keyword. Why is it magic?
(8 answers)
Closed 12 months ago.
In Java, Object takes the place of various class objects, but casting is needed. In the piece of code below, auto seems to be doing the same only that it does not need to be casted with a type variable.
It seems like a lazy (efficient?) way to progress through code. I might get accustomed to using auto instead of a specific type (in the example below, it would be 'int index'). There might be an occasion(s) where such incorporation would not be recommended.
piece of code:
vector<int> fCount(1001,0);
for(auto index : list)
{
++fCount[index];
}

Actually NO. Object in Java differs from auto in C++. In C++, auto keyword only can be applied to any type that can be deduced in compile-time, not run-time. In the code snippet you given, although the index is deduced to be an int, but if you try to use to use index in wherever context that the int cannot be used, compiler will complain.
auto is just a syntactic sugar that modern C++ offers, that you do not have to specify the type when compiler can deduce from the expression. Although you do not have to specify the type explicitly, compiler specifies it in implicit way. After the compiler deduced the type, you cannot use the variable as other type(if it cannot be implictly converted)

Related

How is `auto a(b);` parsed in C++? [duplicate]

This question already has answers here:
C++ auto keyword. Why is it magic?
(8 answers)
Closed 3 years ago.
I came across a curious line of code of the form:
// Given the following definitions:
class B;
B b;
// Line of interest:
auto a(b);
I thought it must be a typo, but after some experimentation found out that it works, and seems to always call the copy constructor of the type of b (even if you have other classes that can also have a matching constructor, and even if you additionally delete the copy constructor for the type of b).
I don't know the technical name of such a statement, so I'm not sure how to search for it in cppreference or StackOverflow. How does the compiler parse this type of statement in general, and where is it documented?
--
Re: duplicate marking. I don't see how they've addressed this construct. I already know auto uses template type deduction, that doesn't clarify anything in this case.
How is auto a(b); parsed in C++?
Depends on what b is. If b is a type, then this is a declaration of function with name a with deduced return type and a single argument of type b.
If b is not a type, then this defines a variable by the name a whose type is deduced from the initialiser.
where is it documented?
The authoritative documentation is the standard document. The standard sections [dcl.type.auto], [dcl.ambig.res], [dcl.fct], [dcl.init] should be relevant.
There are also websites that offer the documentation in (arguably) more approachable manner.
This should also be covered by recent (as in, anything since 2011) introductory C++ books.
and even if you additionally delete the copy constructor for the type of b
I doubt this. Create a mcve.

How do I typedef a function type that returns a function of the same type in C++? [duplicate]

This question already has answers here:
Function Returning Itself
(10 answers)
Closed 3 years ago.
I would like to know how to declare a function type that returns a function of the same type using typedef in C++.
While watching a Golang talk given by Rob Pike on lexical scanning, I came across the following code snippet,
type stateFn func(*Scanner) stateFn
This is the exact behavior that I would like to replicate in C++.
I have tried using the following type definition,
typedef state_fn state_fn(Scanner &);
But this gives me the error function returning function is not allowed.
Is it possible at all to do something like this? If not, how do I do something similar?
You cannot return functions in C++. See [dcl.fct]p10:
Functions shall not have a return type of type array or function, although they may have a return type of type pointer or reference to such things.
However, you can return some form of callable (that may be wrapping a function of the same type).

Is it not type safe if you are allowed to assign an int to a float (or vice-versa)? [duplicate]

This question already has answers here:
Why does C++ allow implicit conversion from int to unsigned int?
(5 answers)
Closed 6 years ago.
Based on what I know about type safety, a type safe language does not allow you to assign a variable of one type to a variable of a different type (unless you perform an explicit conversion). But in C and C++ I can do the following:
int i = 12345;
float f = i; // this is allowed
Is this operation considered not type safe?
[..] a type safe language does not allow you to assign a variable of one type to a variable of a different type (unless you perform an explicit conversion).
I don't think that this would be a good definition. Rather I'd say that a type safe language does not allow operations specific to / designed for a type X to be performed on a value of a other type Y if those two types are not considered compatible under some metric. That's what's known as strong typing (not to be confused with static typing).
Is this operation considered not type safe?
It's a well defined (albeit implicit) conversion from one type to another.
What seems to be bugging you is the part that this conversion is implicit and not explicit. I don't know of a distinct "term" / word to describe that a language allows implicit conversions, though.
That said, C++ surely is not type safe in all respects.

auto variable as a function parameter with > C++11 only [duplicate]

This question already has answers here:
Is auto as a parameter in a regular function a GCC 4.9 extension?
(2 answers)
Closed 6 years ago.
IIUC, I can't use auto variable as a function parameter with C++11, but it might be possible with C++14.
Is this true?
I believe whoever closed it is wrong - gcc-4.9 is not even C++11 compliant and definitely not C++14. So how the answer is helpful in this case?
In a lambda, yes.
A lambda expression can use auto as a function parameter type in C++14.
From the Microsoft Developer Network Lambada Expressions C++
In C++ 14, if the parameter type is generic, you can use the auto keyword as the type specifier. This tells the compiler to create the function call operator as a template. Each instance of auto in a parameter list is equivalent to a distinct type parameter.
auto y = [] (auto first, auto second)
{
return first + second;
};
In C++ 14, ISO C++ still forbids it as a general function parameter. The compiler will give you errors.
In short, you cannot use it in general functions. You may only use it in a lambda function.

Understanding the C++ compiler [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Most vexing parse: why doesn't A a(()); work?
I am having this simple C++ issue that is making me wanna restart my CS degree all over again trying to learn something this time. ;)
Why this code doesn't compile:
vector<int> v(int());
v.push_back(1);
while this other one compiles without a single warning
vector<int> v((int()));
v.push_back(1);
It's even hard to find a difference at all (extra parenthesis were added :P).
It's called the most vexing parse.
vector<int> v(int());
Declares a function v that takes a function (taking no parameters returning an int) and returns a vector<int>. This is automatically "adjusted" to a function v that takes a pointer to a function (taking no parameters returning an int) and returns a vector<int>.
The extra pair of parentheses inhibits this interpretation as you can't place extra parentheses around parameter declarators in function declarations so (int()) can only be interpreted as an initializer for an object named v.
C++ has an explicit disambiguation rule that prefers to parse things (in this case int()) as declarators rather than expressions if it makes syntactic (but not necessarily semantic) sense.
Indeed its a function declaration. See: http://www.gotw.ca/gotw/075.htm