Namespaces
Variants
Views
Actions

Declaring functions

From cppreference.com
< cpp‎ | language
Revision as of 13:50, 25 March 2013 by Cubbi (Talk | contribs)

 
 
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements
Jump statements
Functions
function declaration
lambda function declaration
function template
inline specifier
exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
union types
function types
decltype specifier (C++11)
Specifiers
cv specifiers
storage duration specifiers
constexpr specifier (C++11)
auto specifier (C++11)
alignas specifier (C++11)
Initialization
Literals
Expressions
alternative representations
Utilities
Types
typedef declaration
type alias declaration (C++11)
attributes (C++11)
Casts
implicit conversions
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-style and functional cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
class template
function template
template specialization
parameter packs (C++11)
Miscellaneous
Inline assembly
 

A function declaration introduces the function name and its type. It may appear in any scope, and is commonly placed in header files.

Template:sparam name ( Template:sparam ) ; (1)
Template:sparam name ( Template:sparam ) Template:sparam Template:sparam Template:sparam Template:sparam ; (2)
auto name ( Template:sparam ) Template:sparam Template:sparam Template:sparam Template:sparam -> Template:sparam ; (3) (since C++11)

A function definition provides the body of a function. It may only appear in namespace or class scope.

Template:sparam name ( Template:sparam ) { Template:sparam } (4)
Template:sparam Template:sparam name ( Template:sparam ) Template:sparam Template:sparam Template:sparam Template:sparam Template:sparam try Template:sparam { Template:sparam } Template:sparam (5)
Template:sparam Template:sparam name ( Template:sparam ) Template:sparam Template:sparam Template:sparam Template:sparam -> Template:sparam Template:sparam try Template:sparam { Template:sparam } Template:sparam (6) (since C++11)
Template:sparam Template:sparam name ( Template:sparam ) Template:sparam Template:sparam Template:sparam Template:sparam Template:sparam = 0 ; (7)
Template:sparam Template:sparam name ( Template:sparam ) Template:sparam Template:sparam Template:sparam Template:sparam Template:sparam = default ; (8) (since C++11)
Template:sparam Template:sparam name ( Template:sparam ) Template:sparam Template:sparam Template:sparam Template:sparam Template:sparam = delete ; (9) (since C++11)

Explanation

1) Typical function declaration
2) Comprehensive function declaration
3) Comprehensive function declaration with trailing return type
4) Typical non-member function definition
5) Comprehensive function definition
6) Comprehensive function definition with trailing return type
7) Pure virtual member function
8) Defaulted member function
9) Deleted member function
attr(C++11) - Optional sequence of any number of function attributes, such as [[noreturn]] or [[carries_dependency]]. May appear both before and after the function name
ret - the type returned by the function, may be void if the function returns nothing. Cannot be array or function type, although can be a pointer or reference to such. Required for all functions except constructors, destructors, and conversion operators, which must not provide a return type.
decl - declaration specifier sequence, which consists of none or some of the following keywords: static, extern, inline, virtual, explicit, friend, constexpr, combined with the return type, Template:sparam
cv - Optional const, volatile, or const volatile, only applicable to non-static member functions. For a member function of class T, the type of the this pointer will be const T*, volatile T*, or const volatile T* respectively. A member function declared const cannot modify members of *this.
ref(C++11) - Optional & or &&, only applicable to non-static member functions other than constructors or destructors. For a member function of class T, the type of the implicit object parameter for the purpose of overload resolution will be T& or T&& respectively: a &&-qualified member function can only be called on an rvalue object expression
except - either dynamic-exception-specification or noexcept-specification
virt(C++11) - Optional override or final, only applicable to non-static member functions
->ret(C++11) - Trailing return type, only applicable if Template:sparam is auto. Useful if the type depends on argument names, such as template <class T, class U> auto add(T t, U u) -> decltype(t + u); or is complicated, such as in auto fpif(int)->int(*)(int)
init-list - Constructor initializer list, only used in constructors
try - Optional function try block. If present, Template:sparam must be provided
catch - Optional sequence of catch-blocks, only applicable of try is used.
body - The body of the function, a (possibly empty) compound statement
params - The list of parameters


Example 1: non-member functions

#include <iostream>
#include <string>
 
// declaration in namespace(file) scope
// (the definition is provided later)
int f1();
 
// simple function with a default argument, returning nothing
void f0(const std::string& arg = "world") {
    std::cout << "Hello, " << arg << '\n';
}
 
// function returning a pointer to f0
auto fp11() -> void(*)(const std::string&) {
    return f0;
}
 
// function returning a pointer to f0, pre-C++11 style
void (*fp03())(const std::string&) {
    return f0;
}
 
int main()
{
    f0();
    fp11()("test");
    fp03()("again");
    int f2(std::string); // declaration in function scope
    std::cout << f2("bad12") << '\n';
}
 
// simple non-member function returning int
int f1() {
    return 42;
}
 
// function with an exception specification and a function try block
int f2(std::string str) noexcept try { 
    return std::stoi(str);
} catch(const std::exception& e) {
    std::cerr << "stoi() failed!\n";
    return 0;
}

Output:

Hello, world
Hello, test
Hello, again
stoi() failed!
0


Example 2: member functions

#include <iostream>
#include <string>
#include <utility>
#include <exception>
 
struct S {
    int data;
 
    // simple converting constructor (declaration)
    S(int val);
 
    // simple explicit constructor (declaration)
    explicit S(std::string str);
 
    // const member function (definition)
    virtual int getData() const { return data; }
 
};
 
// definition of the constructor
S::S(int val) : data(val) {
    std::cout << "ctor1 called, data = " << data << '\n';
}
 
// this constructor has a catch clause
S::S(std::string str) try : data(std::stoi(str)) {
    std::cout << "ctor2 called, data = " << data << '\n';
} catch(const std::exception&) {
    std::cout << "ctor2 failed, string was '" << str << "'\n";
    throw; // ctor's catch clause should always rethrow
}
 
struct D : S {
    int data2;
    // constructor with a default argument
    D(int v1, int v2 = 11) : S(v1), data2(v2) {}
 
    // virtual member function
    int getData() const override { return data*data2; }
 
    // lvalue-only assignment operator
    D& operator=(D other) & {
        std::swap(other.data, data);
        std::swap(other.data2, data2);
        return *this;
    }
};
 
int main()
{
    D d1 = 1;
    S s2("2");
    try {
         S s3("not a number");
    } catch(const std::exception&) {}
    std::cout << s2.getData() << '\n';
 
   D d2(3, 4);
   d2 = d1; // OK: assignment to lvalue
//   D(5) = d1; // ERROR: no suitable overload of operator=
}

Output:

ctor1 called, data = 1
ctor2 called, data = 2
ctor2 failed, string was 'not a number'
2
ctor1 called, data = 3