C++ Logo

std-discussion

Advanced search

Re: Const

From: Russell Shaw <rjshaw_at_[hidden]>
Date: Fri, 15 Nov 2024 11:14:36 +1100
On 15/11/24 09:52, Thiago Macieira via Std-Discussion wrote:
> On Thursday 14 November 2024 15:40:02 Mountain Standard Time Russell Shaw via
> Std-Discussion wrote:
>> I can define consts without memory locations like:
>>
>> -------------------------------
>> struct S {
>> static const int x = 0;
>> static const int y = 0;
>> static const int z = 0;
>> };
>>
>> int a = S::x;
>> -------------------------------
>
> Incorrect. If you ODR-use the above, your linker will likely fail.

Hi, g++ doesn't complain:

--------------------------------
#include <iostream>

struct S {
      static const int x = 1;
      static const int y = 2;
      static const int z = 3;
};

int a = S::x; // ok

const int *px = &S::x; // undefined reference to `S::x'

int main()
{
     std::cout << "x is " << S::x << std::endl; // ok

     std::cout << "addr of x is " << &S::x << std::endl;
                            // undefined reference to `S::x'
}
--------------------------------

Received on 2024-11-15 00:14:44