top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is inline specifier in c++?

+1 vote
426 views
What is inline specifier in c++?
posted Jul 31, 2017 by Md Irfan

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

The inline specifier, when used in a function's decl-specifier-seq, declares the function to be an inline function.

A function defined entirely inside a class/struct/union definition, whether it's a member function or a non-member friend function, is implicitly an inline function.

A function declared constexpr is implicitly an inline function.

A deleted function is implicitly an inline function: its (deleted) definition can appear in more than one translation unit.
(since C++11)

The inline specifier, when used in a decl-specifier-seq of a variable with static storage duration (static class member or namespace-scope variable), declares the variable to be an inline variable.

A static member variable (but not a namespace-scope variable) declared constexpr is implicitly an inline variable.
(since C++17)

Description

An inline function or inline variable (since C++17) is a function or variable (since C++17) with the following properties:

1) There may be more than one definition of an inline function or variable (since C++17) in the program as long as each definition appears in a different translation unit and (for non-static inline functions and variables (since C++17)) all definitions are identical. For example, an inline function or an inline variable (since C++17) may be defined in a header file that is #include'd in multiple source files.

2) The definition of an inline function or variable (since C++17) must be present in the translation unit where it is accessed (not necessarily before the point of access).

3) An inline function or variable (since C++17) with external linkage (e.g. not declared static) has the following additional properties:

 It must be declared inline in every translation unit.
 It has the same address in every translation unit.

In an inline function,

Function-local static objects in all function definitions are shared across all translation units (they all refer to the same object defined in one translation unit)

Types defined in all function definitions are also the same in all translation units.

String literals in all function definitions are shared (they are all the same string literal defined in just one translation unit)

Inline const variables at namespace scope have external linkage by default (unlike the non-inline non-volatile const-qualified variables)

The original intent of the inline keyword was to serve as an indicator to the optimizer that inline substitution of a function is preferred over function call, that is, instead of executing the function call CPU instruction to transfer control to the function body, a copy of the function body is executed without generating the call. This avoids overhead created by the function call (copying the arguments and retrieving the result) but it may result in a larger executable as the code for the function has to be repeated multiple times.

Since this meaning of the keyword inline is non-binding, compilers are free to use inline substitution for any function that's not marked inline, and are free to generate function calls to any function marked inline. Those optimization choices do not change the rules regarding multiple definitions and shared statics listed above.

Because the meaning of the keyword inline for functions came to mean "multiple definitions are permitted" rather than "inlining is preferred", that meaning was extended to variables.

Notes

If an inline function or variable (since C++17) with external linkage is defined differently in different translation units, the behavior is undefined.

The inline specifier cannot be used with a function declaration at block scope (inside another function)

The inline specifier cannot re-declare a function that was already defined in the translation unit as non-inline.

The implicitly-generated member functions and any member function declared as defaulted on its first declaration are inline just like any other function defined inside a class definition.

If an inline function is declared in different translation units, the accumulated sets of default arguments must be the same at the end of each translation unit.

In C, inline functions do not have to be declared inline in every translation unit (at most one may be non-inline or extern inline), the function definitions do not have to be identical (but the behavior of the program must not depend on which one is called), and the function-local statics are distinct between different definitions of the same function.

See static data members for additional rules about inline static members

Inline variables eliminate the main obstacle to packaging C++ code as header-only libraries.

Example

// header file
#ifndef EXAMPLE_H
#define EXAMPLE_H
// function included in multiple source files must be inline
inline int sum(int a, int b) 
{
    return a + b;
}
#endif

// source file #2
#include "example.h"
int a()
{
    return sum(1, 2);
}

// source file #1
#include "example.h"
int b()
{
    return sum(3, 4);
}

Credit: http://en.cppreference.com/w/cpp/language/inline

answer Aug 1, 2017 by Manikandan J
Similar Questions
+1 vote

I have written a simple program and stored it with .c file,
When i compiled it with g++ it is getting compiled and giving the proper output when i run that,
but when i compile the same using gcc then it is throwing error.

Below is the sample .c file
#include<stdio.h>
struct A{
        private:
        int a;
        public:
        int sum(){
        a=10;
        }
        void print()
        {
        printf("%d\n",a);
        }
        A()
        {
                printf("Constructor\n");
        }
        A(int b)
        {
                printf("Copy Constructor\n");
        }
        ~A()
        {
                printf("Destructor\n");
        }
};
main()
{
        struct A a(10);
        a.sum();
        a.print();
}
+1 vote

1)I have an inline function with recursion, when i compiled it i am not seeing any Error/Warning for that why?
2)As far as i know its compiler dependent whether to copy inline function or not, but my question is how we will get to know whether compiler has added inline function or not?
3)In What stage of compilation Inline function will be copied?

+2 votes

I would like to know, if there is any difference (in allocated memory space) between defining a function inline in the class declaration (with the inline word), and defining a function inline explicit after the class declaration.

...