can anyone explain me how to use the inline function in c++ code with example?

Recommended Answers

All 6 Replies

use the inline keyword. Note that the compiler can choose not to respect your request.

Thanks for your reply.

The inline functions are complier dependent. But i want to use this inline function in my code. How can i do this in linux?

use the inline keyword. Note that the compiler can choose not to respect your request.

Thanks for your reply.

The inline functions are complier dependent. But i want to use this inline function in my code. How can i do this in linux?

Macro invocations do not perform type checking, or even check that arguments are well-formed, whereas function calls usually do.
In C, a macro cannot use the return keyword with the same meaning as a function would do (it would make the function that asked the expansion terminate, rather than the macro). In other words, you cannot make the macro return something which is not the result of the last expression invoked inside it.

can anyone explain me how to use the inline function in c++ code with example?

There are two ways, actually. The inline keyword is one:

// foo is explicitly inline
inline void foo()
{
    /* ... */
}

Another is for member functions defined inside of a class:

class foo {
public:
    // bar is implicitly inline
    void bar()
    {
        /* ... */
    }
};

But i want to use this inline function in my code.

Why? Inlining involves a cost, and it's a common misconception that there will always be performance improvements. The prevailing advice when it comes to inlining is to avoid it unless you can prove that the benefits outweigh the costs.

Macro invocations do not perform type checking, or even check that arguments are well-formed, whereas function calls usually do.

Preprocessor macros are textual replacements performed prior to compilation. Any type checking and such is performed, just not in a way that's easy to debug. You will[/] get warnings and errors for problematic code, but it's based on the preprocessed code rather than the original source.

In other words, you cannot make the macro return something which is not the result of the last expression invoked inside it.

It gets worse. If you want the macro to have its own scope like a function, you can't return anything at all without using an output variable. This is because the usual tricks for creating a scope don't evaluate to a result that can be used:

#define mymacro() do { \
    // stuff      \
} while(0)

int main()
{
    mymacro();
}

The output variable, while workable, is distinctly less convenient than return values:

#define mymacro(output) do { \
    // stuff        \
    output = result; \
} while(0)

int main()
{
    T rv;

    mymacro(rv);
}

can anyone explain me how to use the inline function in c++ code with example?

Sharing one article for the same
inline article

Above article will help in getting much deeper understanding about it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.