Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Eckel Vol-1, pg 356: 'complicated const'

27 views
Skip to first unread message

Veek M

unread,
Jan 9, 2017, 12:43:10 AM1/9/17
to
http://storage2.static.itmages.com/i/17/0109/h_1483938906_5595082_7e973c6bd8.png

He says that:
1. 'goal of never allocating storage fails with complicated structures'

What does he mean by complicated structure? Is he talking about 'struct'
or a constant expression that's weird?

2. 'const definitions must default to internal linkage' 'linker errors
would occur with complicated consts because they cause storage to be
allocated in multiple cpp files. The linker would see the same
definition in multiple object files, and complain'

How? It's only a definition when a value is assigned. Therefore if C++
did default to external storage, ONLY WHEN I DID:

const int i = 10;
would there be conflict IF it was used in multiple files (.c)

If i just did:
const int i;
and C++ defaults to external storage this would be great because I
wouldn't need 'extern' and it would automagically link to whichever file
had the definition.




Alf P. Steinbach

unread,
Jan 9, 2017, 5:17:06 AM1/9/17
to
On 09.01.2017 06:42, Veek M wrote:
> http://storage2.static.itmages.com/i/17/0109/h_1483938906_5595082_7e973c6bd8.png
>
> He says that:
> 1. 'goal of never allocating storage fails with complicated structures'
>
> What does he mean by complicated structure? Is he talking about 'struct'
> or a constant expression that's weird?

A goal of never allocating storage appears to be a goal of defining a
name for a value so that the compiler can inline use of that value
instead of referring to a memory location where the value is stored.

In modern C++ this can be expressed with `constexpr`.

`constexpr` can't handle e.g. a `std::string`, because it uses dynamic
allocation which necessarily is done at run-time, while the `constexpr`
mechanism is entirely compile time.


> 2. 'const definitions must default to internal linkage' 'linker errors
> would occur with complicated consts because they cause storage to be
> allocated in multiple cpp files. The linker would see the same
> definition in multiple object files, and complain'
>
> How? It's only a definition when a value is assigned. Therefore if C++
> did default to external storage, ONLY WHEN I DID:
>
> const int i = 10;
> would there be conflict IF it was used in multiple files (.c)
>
> If i just did:
> const int i;
> and C++ defaults to external storage this would be great because I
> wouldn't need 'extern' and it would automagically link to whichever file
> had the definition.

You can't do the latter, it won't compile.

But I'm not sure what Bruce is talking about here, I think I'd have to
look that up in context to get some traction on it.


Possibly relevant: Monthy Python's sketch about Bruce the Australian.
<url: https://www.youtube.com/watch?v=bNBy1D1Y0h4>

Cheers!,

- Alf


0 new messages