Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Since which version of C++ are default arguments allowed?

Tags:

c++

c++11

Example:

void foo(int a,int b=12) {
...
}

Since which version of C++ is this legal? Was it introduced in C++11?

like image 264
powerpete Avatar asked Jan 22 '19 14:01

powerpete


People also ask

Is there default argument in C?

Generally no, but in gcc You may make the last parameter of funcA() optional with a macro.

How do you declare a default argument in C?

A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the calling function doesn't provide a value for the argument. In case any value is passed, the default value is overridden.

What type are C functions by default?

By default, C uses call by value to pass arguments.

Does C++ support default arguments?

In C++ programming, we can provide default values for function parameters. If a function with default arguments is called without passing arguments, then the default parameters are used. However, if arguments are passed while calling the function, the default arguments are ignored.


1 Answers

Per Bjarne's "History of C++" (see page 6), default arguments were added in the very first version of the very first incarnation of C++, C With Classes, whose "spec" (if you can call it that) was published back in 1980.

They remained present through to initial standardisation in 1998, and remain present to this day.

In other words, every version of C++ since the dawn of time has supported default arguments.

In other words: literally forever. :)

like image 158
Lightness Races in Orbit Avatar answered Oct 21 '22 19:10

Lightness Races in Orbit