C++ arguments declaration after the signature [duplicate] - c++

This question already has answers here:
C function syntax, parameter types declared after parameter list
(7 answers)
Closed 7 years ago.
I know that in C/C++ you declare function arguments like this:
type functionName(type arg1,type arg2 )
{
...code...
}
However there are a few places, for example here http://www.enderunix.org/docs/eng/daemon.php
Where I see that a function can also be declared like this
type functionName(arg1, arg2)
type arg1;
type arg2;
{
...code..
}
Is that a valid declaration?
I've never seen anything like that in any C/C++ manual, can anyone tell me if this is correct, what are the pros and cons for this type of declaration if it is valid, and if possible point me to some manual or document that explains this.
Thanks

This is old style K&R C. Not sure it will work for C++.

This is C (k&r)
Not valid c++

Related

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

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)

Ampersands in C++ method Parameters: Diff between SomeType& param1 and SomeType &param1 [duplicate]

This question already has answers here:
Where ampersand "&" can be put when passing argument by reference?
(4 answers)
Closed 2 years ago.
C++ newbie here, could someone please let me know the difference between the two following method signatures?
void AddConcert(const Concert &concert);
and
void AddConcert(const Concert& concert);
Thanks!
Preference. The compiler treats them the same
Actually there are no difference between them.
We have const Concert& concert, for example, you read it like: reference to a const Concert variable.
So the second is the same.

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).

C# extension methods analogue in C++ [duplicate]

This question already has answers here:
Extension methods in c++
(7 answers)
C++ Class Extension
(9 answers)
Closed 5 years ago.
C# has this little nice feature: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods
This is really cool. Let me give you an example:
I want to add concat method to std::vector but I don't want to inherit it. This feature would be very useful. Do you have any analogues feature in C++ that allows to add a function to a type without inheriting from the original type? I am asking for a language-level feature, please.
In C++ this is not directly possible; that being said, you could implement a function that has a reference to a std::vector as a first argument, serving a similar purpose as an extension method (which more or less are just syntactic sugar for that).
A free function might be your friend here e.g.
namespace VectorMethods
{
std::string contat(const std::vector<std::string>& vec)
{
// return result of concatenating vector
}
}

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.