Friday, October 14, 2022

Digest for comp.lang.c++@googlegroups.com - 8 updates in 4 topics

"Alf P. Steinbach" <alf.p.steinbach@gmail.com>: Oct 14 08:52PM +0200

On 14 Oct 2022 17:50, Frederick Virchanza Gotham wrote:
> Instruments compiler. The manual says that where you have a static
> variable inside an inline function, there are multiple copies of the
> static variable (one for each translation unit).
 
I would first of all /test/ that. I would be a weird compiler to have
such a bug and have it documented as intended behavior.
 
 
> SomeType &GetObj(void) { __asm("ABC: .bss mybuf, 64);
 
> return *static_cast<SomeType*>(static_cast<void*>(buf)); }
 
> I still haven't got this working yet. Anyone got any ideas?
 
C++03 had two mechanisms for discarding duplicates at link time:
`inline` for functions, and `template` in general.
 
Unless the compiler has a bug (documented or not) for that too you can
write e.g.
 
 
namespace my {
using std::string;
 
template< class >
struct Foo_
{
static string message;
};
 
template< class Dummy >
string Foo_<Dummy>::message = "unassigned";
 
inline string& message() { return Foo_<void>::message; }
} // namespace my
 
 
- Alf
Marcel Mueller <news.5.maazl@spamgourmet.org>: Oct 14 08:56PM +0200

Am 14.10.22 um 17:50 schrieb Frederick Virchanza Gotham:
> Usually we put all the declarations of objects in a header file, and all the definitions of objects in a source file.
 
> At the moment though I'm writing a 'universal header file' and so I want to put everything in the header file. There will be no accompanying source file.
 
Don't do that.
 
 
> SomeType obj;
> return obj;
> }
 
This is undefined behavior because of a dangling reference. The compiler
should warn you about this fault.
 
 
> This works fine -- however there is a problem with the Texas Instruments compiler. The manual says that where you have a static variable inside an inline function, there are multiple copies of the static variable (one for each translation unit).
 
> So now I'm trying to devise a way of making this work for the Texas Instruments compiler too, maybe something like the following:
 
If the TI compiler has no weak linker, you will _never_ succeed.
You cannot deduplicate storage without global symbols. And you cannot
have global symbols defined in different translation units unless you
have a weak linker.
 
As long as you are only talking about code a compiler w/o weak symbols
might simply duplicate the code for each translation unit. But this does
not work for storage.
 
 
> return *static_cast<SomeType*>(static_cast<void*>(buf));
> }
 
> I still haven't got this working yet. Anyone got any ideas?
 
This will either generate one slot for each translation unit if the
symbol ABC is not global or a linker error if the symbol is global.
 
To get the desired result the symbol needs to be weak and the storage
needs its own segment in the object file.
 
 
You have two options:
- Either do not use static storage at all. Instance storage is managed
by the one who calls the constructor.
- Or use a dedicated translation unit for your storage as anyone else
does. The latter could be placed in a static library to simplify things.
 
 
Marcel
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>: Oct 14 02:40PM -0700

On 10/14/2022 11:56 AM, Marcel Mueller wrote:
>> want to put everything in the header file. There will be no
>> accompanying source file.
 
> Don't do that.
[...]
 
Perhaps I am misunderstanding your main point, however, there are some
nice "header only" libraries. GLM comes to mind:
 
https://github.com/g-truc/glm
Frederick Virchanza Gotham <cauldwell.thomas@gmail.com>: Oct 14 04:14PM -0700

On Friday, October 14, 2022 at 7:52:25 PM UTC+1, Alf wrote:
 
> inline string& message() { return Foo_<void>::message; }
 
 
Thanks Alf, the following compiled and linked fine for me:
 
template<class> struct Foo { static SimpleCrypto128 obj; };
template<class Dummy> SimpleCrypto128 Foo<Dummy>::obj;
inline SimpleCrypto128 &Scrypt128(void) { return Foo<void>::obj; }
 
I hadn't bothered trying using templates for this, because I figured if the compiler couldn't handle static variables in simple inline functions, then how could it cope with a template function. But anyway it has compiled and linked for me. Of course I'll have to test it to make sure there's only one object, but so far it looks good.
 
As for the peculiarities of the Texas Instruments compiler, well they at least have a finite list of its non-compliance. I've copy-pasted the following from the compiler manual:
 
6.2 Characteristics of TMS320C28x C++
 
The C28x compiler supports C++ as defined in the ANSI/ISO/IEC 14882:2003 standard (C++03), including these
features:
 
• Complete C++ standard library support, with exceptions noted below.
• Templates
• Exceptions, which are enabled with the --exceptions option; see Section 6.6.
• Run-time type information (RTTI), which can be enabled with the --rtti compiler option.
 
The compiler supports the 2003 standard of C++ as standardized by the ISO. However, the following features
are not implemented or fully supported:
 
• The compiler does not support embedded C++ run-time-support libraries.
• The library supports wide chars (wchar_t), in that template functions and classes that are defined for char are
also available for wchar_t. For example, wide char stream classes wios, wiostream, wstreambuf and so on
(corresponding to char classes ios, iostream, streambuf) are implemented. However, there is no low-level file
I/O for wide chars. Also, the C library interface to wide char support (through the C++ headers <cwchar> and
<cwctype>) is limited as described above in the C library.
• If the definition of an inline function contains a static variable, and it appears in multiple compilation units
(usually because it's a member function of a class defined in a header file), the compiler generates multiple
copies of the static variable rather than resolving them to a single definition. The compiler emits a warning
(#1369) in such cases.
• The export keyword is not implemented.
Nikki Locke <nikki@trumphurst.com>: Oct 14 10:23PM

Available C++ Libraries FAQ
 
URL: http://www.trumphurst.com/cpplibs/
 
This is a searchable list of libraries and utilities (both free
and commercial) available to C++ programmers.
 
If you know of a library which is not in the list, why not fill
in the form at http://www.trumphurst.com/cpplibs/cppsub.php
 
Maintainer: Nikki Locke - if you wish to contact me, please use the form on the website.
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>: Oct 14 03:44PM -0700

On 10/14/2022 3:23 PM, Nikki Locke wrote:
 
> If you know of a library which is not in the list, why not fill
> in the form at http://www.trumphurst.com/cpplibs/cppsub.php
 
> Maintainer: Nikki Locke - if you wish to contact me, please use the form on the website.
 
Perhaps you should show the list to the vcpkg guys over at Microsoft:
 
https://vcpkg.io/en/index.html
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>: Oct 14 02:32PM -0700

On 10/14/2022 1:07 AM, Andrey Tarasevich wrote:
 
>> Thanks !
 
>> And that method is deprecated already.
 
> It is deprecated because there's no such thing as "POD" in C++ anymore.
 
Well, that certainly sucks.
 
Mr Flibble <flibble@reddwarf.jmc.corp>: Oct 14 09:01PM +0100

On Fri, 14 Oct 2022 14:58:54 -0000 (UTC)
> With embedded development you often literally have to worry about
> every byte you use both in ROM and RAM and whether the mainloop is
> going to be fast enough to do its job.
 
In C++ you don't pay for what you don't use: in the case of a C++ class
template only the member functions instantiated will exist in the text
segment.
 
/Flibble
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: