Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About func(const int&) and func(const int)

Tags:

c++

c++11

#include <iostream>

class Account {
public:
    static double GetCircumference(const double &dR) { return 2 * dR * 3.1415926; }
    static constexpr double cd = 3.0;
};

// constexpr double Account::cd;

int main()
{
    std::cout << Account::GetCircumference(Account::cd) << std::endl;
}

The code is wrong unless I remove the "//". But if I only change (const double &dR) to (const double dR), it becomes ok too. Why?

like image 905
Mikato Avatar asked Dec 22 '21 09:12

Mikato


People also ask

What is const int function?

int const function(parameters) The const is redundant. Declaring a simple type such as int as a const return value is not useful, although it may help to make the code more self-documenting. If the return type is a reference or pointer however then the story changes.

What is the function of const?

The const member functions are the functions which are declared as constant in the program. The object called by these functions cannot be modified. It is recommended to use const keyword so that accidental changes to object are avoided. A const member function can be called by any type of object.

What does const int mean in code?

In the type int const , const is a type qualifier, and int is a type specifier. Here, const qualifies the type int . Qualifiers change the semantics of the type in some way. Other type qualifiers include volatile and restrict . (Note you can also write const int , which means the same thing as int const .

What does const int mean in C++?

The const keyword allows you to specify whether or not a variable is modifiable. You can use const to prevent modifications to variables and const pointers and const references prevent changing the data pointed to (or referenced).


Video Answer


1 Answers

In C++11, this in-class declaration:

static constexpr double cd = 3.0;

is not a definition (... until C++17; after which constexpr static data members are implicitly inline).

This is a out-of-class definition:

constexpr double Account::cd;

A definition is is needed if Account::cd is odr-used, which it is if it is passed to:

double GetCircumference(const double &dR);

as its reference is taken.

[basic.def.odr]/3 Every program shall contain exactly one definition of every non-inline function or variable that is odr-used in that program; no diagnostic required. [...]

However, if it is passed to

double GetCircumference(const double dR);

it is not odr-used:

[basic.def.odr]/2 An expression is potentially evaluated unless it is an unevaluated operand (Clause [expr]) or a subexpression thereof. A variable whose name appears as a potentially-evaluated expression is odr-used unless it is an object that satisfies the requirements for appearing in a constant expression ([expr.const]) and the lvalue-to-rvalue conversion ([conv.lval]) is immediately applied.

and a definition is not required.

like image 100
dfrib Avatar answered Oct 17 '22 11:10

dfrib