Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

#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?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading