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

Compile-time constructors?

47 views
Skip to first unread message

Rick C. Hodgin

unread,
Nov 12, 2018, 3:43:38 PM11/12/18
to
Is there a concept in C++ of a compile-time constructor, such that
it does not generate code to call the constructor function at in-
stantiation, but merely allocates the class's required block size,
and then does a memcpy() from a compile-time pre-computed constructor
block?

If there were no variables unknown at compile-time regarding how the
object would instantiate, that would seem to be a path to a great
potential saving in time if you had a lot of objects to instantiate.

--
Rick C. Hodgin

Mr Flibble

unread,
Nov 12, 2018, 3:47:15 PM11/12/18
to
I suspect you already know the answer to that question and you are asking
it as an excuse to plug your "CAlive" nonsense again. Please. Fuck. Off.

/Flibble

--
“You won’t burn in hell. But be nice anyway.” – Ricky Gervais

“I see Atheists are fighting and killing each other again, over who
doesn’t believe in any God the most. Oh, no..wait.. that never happens.” –
Ricky Gervais

"Suppose it's all true, and you walk up to the pearly gates, and are
confronted by God," Bryne asked on his show The Meaning of Life. "What
will Stephen Fry say to him, her, or it?"
"I'd say, bone cancer in children? What's that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery
that is not our fault. It's not right, it's utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates a
world that is so full of injustice and pain. That's what I would say."

Rick C. Hodgin

unread,
Nov 12, 2018, 3:54:28 PM11/12/18
to
On Monday, November 12, 2018 at 3:47:15 PM UTC-5, Mr Flibble wrote:
> On 12/11/2018 20:43, Rick C. Hodgin wrote:
> > Is there a concept in C++ of a compile-time constructor, such that
> > it does not generate code to call the constructor function at in-
> > stantiation, but merely allocates the class's required block size,
> > and then does a memcpy() from a compile-time pre-computed constructor
> > block?
> >
> > If there were no variables unknown at compile-time regarding how the
> > object would instantiate, that would seem to be a path to a great
> > potential saving in time if you had a lot of objects to instantiate.
>
> I suspect you already know the answer to that question and you are asking
> it as an excuse to plug your "CAlive" nonsense again...

I do not know the answer. I think I do, but I am not certain.
Sound familiar, Leigh? And yes,I was considering adding it to
a compiler project I'm in pursuit. Not sure yet though. It
would be a potentially difficult analysis to determine if it
truly is static depending on its inheritance.

--
Rick C. Hodgin

David Brown

unread,
Nov 12, 2018, 4:15:10 PM11/12/18
to
Construction can be as simple or complicated as you want. If the
construction is no more than setting fixed known values into the data
members, then the compiler can do it by individual stores or a general
memcpy - whatever it likes.

Rick C. Hodgin

unread,
Nov 12, 2018, 4:20:10 PM11/12/18
to
On Monday, November 12, 2018 at 4:15:10 PM UTC-5, David Brown wrote:
> Construction can be as simple or complicated as you want. If the
> construction is no more than setting fixed known values into the data
> members, then the compiler can do it by individual stores or a general
> memcpy - whatever it likes.

Is this done in practice? Is there something in the standard
which allows for it to do this, without actually calling the
constructor function at runtime?

--
Rick C. Hodgin

Scott Lurndal

unread,
Nov 12, 2018, 4:43:02 PM11/12/18
to

Rick C. Hodgin

unread,
Nov 12, 2018, 4:59:40 PM11/12/18
to
Thank you, Scott.

--
Rick C. Hodgin

David Brown

unread,
Nov 12, 2018, 4:59:49 PM11/12/18
to
Compilers can handle this in any way they want. It's unlikely that
optimising compilers would call a memcpy routine - they rarely call
memcpy routines even when you write "memcpy(...)" if the size is known
at compile time.

As Scott says, there is the "as if" rule - as long as the visible
results are as if the compiler had slavishly translated statement for
statement, there compiler can optimise as it sees fit.

Certainly constructor functions are often inlined - if their definitions
are known when they are used, and the code is fairly short.

Alf P. Steinbach

unread,
Nov 12, 2018, 6:23:20 PM11/12/18
to
A "compile time constructor" is simply a `constexpr` constructor.

Cheers!,

- Alf


Rick C. Hodgin

unread,
Nov 13, 2018, 6:14:17 AM11/13/18
to
On Monday, November 12, 2018 at 6:23:20 PM UTC-5, Alf P. Steinbach wrote:
> A "compile time constructor" is simply a `constexpr` constructor.

Is that a user-specified thing, or a compiler-determined thing?

--
Rick C. Hodgin
0 new messages