Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
FamDav
Mar 29, 2008
EDIT: Solved.

FamDav fucked around with this message at 06:31 on Jul 26, 2012

Adbot
ADBOT LOVES YOU

That Turkey Story
Mar 30, 2003

rjmccall posted:

Critically, that type cannot be copyable, or you'll immediately lose your const correctness; instead, you have to make instances of some other shared pointer type from it.
I don't agree with the statement that the type "cannot be copyable." All well-defined types have a notion of copyability whether or not they have the use-case for implementing them. In this case for instance, the tree-like structure where the constness propagates to children, a copying of that portion of the tree would make sense when copying the parent. By propagating constness you are effectively saying that the state of the children is a part of the state of the parent, so it should therefore be copied when the parent is copied. I don't see a need for shared ownership here nor for noncopyability.

Sean Parent has given some awesome talks on value semantics, including how all types are inherently regular (which includes copyability).

Poor quality video on a talk from this year:
https://www.youtube.com/watch?v=_BpMYeUFXv8

Rottbott
Jul 27, 2006
DMC
That was very interesting. Over the last few years I've been finding myself leaning towards creating only 'regular' types without trying to be clever about copying or shared ownership, particularly since I got r-value references.

However sometimes sharing seems unavoidable. For example, if I load a texture off disk, I don't want to have to reload it or copy it due to limited memory. But if two unrelated parts of a game happen to require the same texture, I need to share that texture object. It seems a perfect case for shared pointers (in fact I have yet another 'smart pointer' called Resource<T> which manages IO in addition to the ref counting paraphernalia). I'm happy with how all that works, unless someone has a better idea than shared ownership - I'm still having a think about the copy-on-write stuff from that talk. A texture is of course perfectly copyable and I do allow that using regular copy semantics, if that's what you really want to do.

But all I'm trying to achieve with the const-propogation part is a solution to this simple situation:

code:
// Start with this
struct Model
{
	const Texture& GetTexture() const { return m_Texture; }
	Texture& GetTexture() { return m_Texture; }

	void Render() const
	{
		m_Texture.Modify(); // Won't compile, as expected
		// ...
	}

	Texture m_Texture;
}

// But then change to shared ownership
struct Model
{
	const Texture& GetTexture() const { return m_Texture; }
	Texture& GetTexture() { return m_Texture; }

	void Render() const
	{
		m_Texture->Modify(); // Will compile
		// ...
	}

	Resource<Texture> m_Texture;
}

Cross_
Aug 22, 2008
I have been somewhat out of the loop regarding C++ changes for the past 5 years or so (doing Java & Obj-C stuff). What is a good book that covers the C++ 11 and TR1 changes ?

shrughes
Oct 11, 2008

(call/cc call/cc)

Cross_ posted:

I have been somewhat out of the loop regarding C++ changes for the past 5 years or so (doing Java & Obj-C stuff). What is a good book that covers the C++ 11 and TR1 changes ?

The Wikipedia entry.

OneEightHundred
Feb 28, 2008

Soon, we will be unstoppable!
Has anyone been messing with Visual Studio 2012 Express RC?

I'm trying to find out if they removed the Memory and Thread windows. They documented them as unavailable in Visual C++ 2010, but you could still access them with Expert Settings enabled. The likelihood of that being a mistake combined with their supposed original plan to drop C++ support from Express entirely has me wondering if they'll still be there next round.

peepsalot
Apr 24, 2007

        PEEP THIS...
           BITCH!

I have Visual Studio 2010 that I use for mostly C# stuff, but there is one C project I work with that I would like to move into Visual Studio IDE for easier code navigation.

This project is currently built using a batch file, that calls out to some special gcc versions that target an embedded device.

What I'm wondering is if there is a way to just specify my own entire build process in VS2010 to just use this batch file or something similar? I see that you can add build steps before/after the built in ones, but I think I want to replace the built-in build steps all together.

nielsm
Jun 1, 2009



peepsalot posted:

What I'm wondering is if there is a way to just specify my own entire build process in VS2010 to just use this batch file or something similar? I see that you can add build steps before/after the built in ones, but I think I want to replace the built-in build steps all together.

Create a "Makefile Project" instead of a "Win32 Project". I don't really know where to go from there, but it's how you'd get started.
I'm not sure if much of the fancy features will work if you aren't using Microsoft's toolset.

Lexical Unit
Sep 16, 2003

I'm trying to get a working setup of llvm+clang using libc++ headers for compiling a library (libpqxx) using c++11. I'd try to take this question to a mailing list but I'm not even sure which mailing list would be the right one to go to in this case (llvm? clang? libc++? libpqxx?) because I have no idea if I even have things set up properly. I'm on OS X Lion. This is what I've done:

Edit: I can't figure out how to get the forums to not insert [url] tags...
Bash code:
unset CXX
unset CXXFLAGS
unset LDFLAGS
unset LIBS
unset CPPFLAGS
unset CC
unset CFLAGS
unset CPP
unset CXXCPP

mkdir -p ~/.clang+llvm+libc++
cd ~/.clang+llvm+libc++

svn co [url]http://llvm.org/svn/llvm-project/llvm/trunk[/url] llvm

pushd llvm/tools
svn co [url]http://llvm.org/svn/llvm-project/cfe/trunk[/url] clang
popd

pushd llvm/projects
svn co [url]http://llvm.org/svn/llvm-project/compiler-rt/trunk[/url] compiler-rt
svn co [url]http://llvm.org/svn/llvm-project/libcxx/trunk[/url] libcxx 
popd

mkdir -p build
pushd build
../llvm/configure --enable-optimized
make update # just to be sure
make -j $(( $(system_profiler | grep -i "Total Number of Cores" | cut -d: -f2) + 1))
popd

pushd build/Release+Asserts/bin
export PATH="$(pwd):$PATH"
export CXX="$(pwd)/clang++"
export CC="$(pwd)/clang"
popd

pushd llvm/projects/libcxx
export CXXFLAGS="-std=c++11 -nostdinc++ -I$(pwd)/include"
export LDFLAGS="-L$(pwd)/lib"
popd

pushd llvm/projects/libcxx/lib
export TRIPLE=-apple-
./buildit # gives some warnings but no errors
export CXXFLAGS="$CXXFLAGS"
export LDFLAGS="$LDFLAGS -L$(pwd)"
export LIBS="-lc++"
popd
Following all this, my current environment is:
Bash code:
unset CPPFLAGS
unset CFLAGS
unset CPP
unset CXXCPP
export PATH="$HOME/.clang+llvm+libc++/build/Release+Asserts/bin:$PATH"
export CXX="$HOME/.clang+llvm+libc++/build/Release+Asserts/bin/clang++"
export CC="$HOME/.clang+llvm+libc++/build/Release+Asserts/bin/clang"
export CXXFLAGS="-std=c++11 -nostdinc++ -I$HOME/.clang+llvm+libc++/llvm/projects/libcxx/include"
export LIBS="-lc++"
export LDFLAGS="-L$HOME/.clang+llvm+libc++/llvm/projects/libcxx/lib -L$HOME/.clang+llvm+libc++/llvm/projects/libcxx/lib"
As a small test I tried to see if I could get a hello world application to build and run:
Bash code:
cat <<EOF > test.cpp
#include <iostream>
int main() { std::cout << "Hello, world!" << std::endl; }

EOF
clang++ $CXXFLAGS $LIBS $LDFLAGS -Wall -Werror test.cpp && ./a.out # prints "Hello, world!"
So far so good!

Now on to libpqxx:
Bash code:
cd /tmp
curl -O [url]http://pqxx.org/download/software/libpqxx/libpqxx-4.0.tar.gz[/url]
tar xvzf libpqxx-4.0.tar.gz
cd libpqxx-4.0
./configure
make
At first I got a compile error based on an ambiguous call to pqxx::largeobjectaccess. This seems like a bug in their code due to a combination of default parameters and overloading, I fixed the error with:
code:
--- include/pqxx/largeobject.hxx	2012-08-02 15:58:36.000000000 -0500
+++ /tmp/fix/largeobject.hxx	2012-08-02 15:58:40.000000000 -0500
@@ -396,7 +396,7 @@
 			openmode mode = PGSTD::ios::in | PGSTD::ios::out,
 			size_type BufSize=512) :			//[t48]
     m_BufSize(BufSize),
-    m_Obj(T, O),
+    m_Obj(T, O, mode),
     m_G(0),
     m_P(0)
 	{ initialize(mode); }
@@ -406,7 +406,7 @@
 			openmode mode = PGSTD::ios::in | PGSTD::ios::out,
 			size_type BufSize=512) :			//[t48]
     m_BufSize(BufSize),
-    m_Obj(T, O),
+    m_Obj(T, O, mode),
     m_G(0),
     m_P(0)
 	{ initialize(mode); }
Then I got another compile error:
strconv.cxx:195:10: error: assigning to 'bool' from incompatible type 'std::__1::basic_istream<char>'
which I fixed by casting the expression to bool before the assignment.

Then finally I tried to compile and got an error that I can't figure out coming from a libc++ header:
code:
In file included from rmlo.cxx:2:
In file included from $HOME/.clang+llvm+libc++/llvm/projects/libcxx/include/iostream:38:
In file included from $HOME/.clang+llvm+libc++/llvm/projects/libcxx/include/ios:216:
In file included from $HOME/.clang+llvm+libc++/llvm/projects/libcxx/include/__locale:15:
In file included from $HOME/.clang+llvm+libc++/llvm/projects/libcxx/include/string:434:
In file included from $HOME/.clang+llvm+libc++/llvm/projects/libcxx/include/algorithm:594:
In file included from $HOME/.clang+llvm+libc++/llvm/projects/libcxx/include/memory:599:
In file included from ../include/pqxx/tuple:19:
In file included from ../include/pqxx/tuple.hxx:25:
In file included from ../include/pqxx/except:19:
In file included from ../include/pqxx/except.hxx:27:
In file included from ../include/pqxx/util:18:
In file included from ../include/pqxx/util.hxx:28:
In file included from $HOME/.clang+llvm+libc++/llvm/projects/libcxx/include/vector:261:
$HOME/.clang+llvm+libc++/llvm/projects/libcxx/include/__bit_reference:160:38: error: no member named 'min' in namespace 'std::__1'
        __storage_type __dn = _VSTD::min(__clz_f, __n);
                              ~~~~~~~^
$HOME/.clang+llvm+libc++/llvm/projects/libcxx/include/__bit_reference:164:69: error: no member named '__ctz' in namespace 'std::__1'
            return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__ctz(__b)));
                                                             ~~~~~~~^
$HOME/.clang+llvm+libc++/llvm/projects/libcxx/include/__bit_reference:171:69: error: no member named '__ctz' in namespace 'std::__1'
            return _It(__first.__seg_, static_cast<unsigned>(_VSTD::__ctz(*__first.__seg_)));

... lots of similar errors ...
Here's the offending command:
$HOME/.clang+llvm+libc++/build/Release+Asserts/bin/clang++ -DHAVE_CONFIG_H -I. -I../include/pqxx -I../include -I../include -I/usr/include -std=c++11 -nostdinc++ -I$HOME/.clang+llvm+libc++/llvm/projects/libcxx/include -MT rmlo.o -MD -MP -MF .deps/rmlo.Tpo -c -o rmlo.o rmlo.cxx


At this point I'm not sure what to do. Shouldn't min() be in the std namespace? Please help! :smith:

Lexical Unit fucked around with this message at 22:16 on Aug 2, 2012

Pollyanna
Mar 5, 2005

Milk's on them.


I'm running OSX, what is a good IDE for C++? I tried using Eclipse, but I can't get anything to work, not even the sample Hello World program...

Lexical Unit
Sep 16, 2003

Pollyanna posted:

I'm running OSX, what is a good IDE for C++? I tried using Eclipse, but I can't get anything to work, not even the sample Hello World program...
Sublime Text 2 is amazing and has completely replaced vim for me. You can even use this plugin to get auto complete suggestions using clang++ :D

Pollyanna
Mar 5, 2005

Milk's on them.


Lexical Unit posted:

Sublime Text 2 is amazing and has completely replaced vim for me. You can even use this plugin to get auto complete suggestions using clang++ :D

Apparently it needs 10.6.8 to run and I have no idea where to find a copy of Snow Leopard :bang: God, I miss Windows.

Volte
Oct 4, 2004

woosh woosh

Pollyanna posted:

Apparently it needs 10.6.8 to run and I have no idea where to find a copy of Snow Leopard :bang: God, I miss Windows.
It works on Lion and Mountain Lion too :confused:

Pollyanna
Mar 5, 2005

Milk's on them.


Volte posted:

It works on Lion and Mountain Lion too :confused:

I have 10.5.8. This has been a problem for a long time and my computer is dying anyway. I'm considering getting a new one.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Lexical Unit posted:

At this point I'm not sure what to do. Shouldn't min() be in the std namespace? Please help! :smith:

Your include stack suggests that there are headers in pqxx which are hijacking libc++'s includes. If you can't remove those, you might try using -isystem for the libc++ path.

Lexical Unit
Sep 16, 2003

rjmccall posted:

Your include stack suggests that there are headers in pqxx which are hijacking libc++'s includes. If you can't remove those, you might try using -isystem for the libc++ path.
Oh gross, I didn't even notice that. That's definitely what's going on. I tried replacing -I with -isystem for the libc++ headers but I still got the same error. Is -isystem documented somewhere? I can't find it on the man page.

I was able to get the library to compile though, I just had to delete <pqxx/tuple> (which all it did was include <pqxx/tuple.hxx>) and replace the lines where it was included with tuple.hxx instead.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
The man page is terrible; it's a known bug that we have definite plans to address.

I really shouldn't have been surprised that -isystem didn't help, though. Headers are terrible.

Precambrian Video Games
Aug 19, 2002



Pollyanna posted:

I'm running OSX, what is a good IDE for C++? I tried using Eclipse, but I can't get anything to work, not even the sample Hello World program...

I've been using the Eclipse CDT for years on Linux and I'm about to give up. Every new release seems to break existing features in imaginative ways and there's just no hope in trying to debug it.

Google searching leads me to believe that Netbeans is a decent alternative so give that a shot.

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip
Goodness gracious Netbeans and Eclipse are not C++ IDEs do not use them for C++, you would be more productive in EDIT.COM

Tengort
Sep 4, 2009

OneEightHundred posted:

Has anyone been messing with Visual Studio 2012 Express RC?

I'm trying to find out if they removed the Memory and Thread windows. They documented them as unavailable in Visual C++ 2010, but you could still access them with Expert Settings enabled. The likelihood of that being a mistake combined with their supposed original plan to drop C++ support from Express entirely has me wondering if they'll still be there next round.

They're there in the default settings.

Precambrian Video Games
Aug 19, 2002



Otto Skorzeny posted:

Goodness gracious Netbeans and Eclipse are not C++ IDEs do not use them for C++, you would be more productive in EDIT.COM

I guess I just imagined the last 4 years of the Eclipse CDT's existence then. :rolleyes:

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

eXXon posted:

I guess I just imagined the last 4 years of the Eclipse CDT's existence then. :rolleyes:

I found it really hard to believe that both Netbeans and Eclipse made IDEs that supported C++ until I googled it myself. Why god why did the world need more [probably, I have no idea and no urge to find out] lovely C++ IDEs?

Flobbster
Feb 17, 2005

"Cadet Kirk, after the way you cheated on the Kobayashi Maru test I oughta punch you in tha face!"

hieronymus posted:

I found it really hard to believe that both Netbeans and Eclipse made IDEs that supported C++ until I googled it myself. Why god why did the world need more [probably, I have no idea and no urge to find out] lovely C++ IDEs?

The cross-platform aspect of it can be nice sometimes.

I teach a C++ programming course from time to time to non-CS majors, many of whom have never done any programming before. (Don't get me started on why C++ is a terrible language for this, I teach what I'm assigned.)

Since these students can barely do anything more with a computer than Facebook, I'm certainly not going to teach them "make" or anything remotely close to the terminal.

Using Netbeans or Eclipse at least means I only have to write one set of instructions for both Windows and Mac users. Both IDEs have their bumpy parts, but it works well enough.

Volte
Oct 4, 2004

woosh woosh
Netbeans and Eclipse are probably the best C++ IDEs other than Visual Studio. Eclipse CDT has been around for at least 10 years and Netbeans has supported C++ for 4-5 years also. Personally I prefer just using a nice text editor but if you really need IDE features, there's nothing wrong with Netbeans or Eclipse.

Crankit
Feb 7, 2011

HE WATCHES
How does CodeBlocks compare to NetBeans / Eclipse and vice-versa?
I've used the MS tools and I've used CodeBlocks on a linux machine very briefly but I've not used NetBeans or Eclipse.

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

Crankit posted:

How does CodeBlocks compare to NetBeans / Eclipse and vice-versa?
I've used the MS tools and I've used CodeBlocks on a linux machine very briefly but I've not used NetBeans or Eclipse.

Well I like CodeBlocks over Eclipse. NetBeans can make proper Makefiles though if you're lazy, CodeBlocks needs a plugin to do so.

UraniumAnchor
May 21, 2006

Not a walrus.
So I noticed something odd while trying to fix a bug today, using Android NDK r8b (GCC 4.6.x):

code:
bool b;
char *c = (char *)&b;
*c = 152;
while (true) {
	cout << b << endl;
	b = !b;
}
code:
152
153
152
153
152
153
Undefined behavior, or genuine compiler bug?

MrMoo
Sep 14, 2000

Isn't BOOL only a char in Win32 whilst _Bool is an int for everyone else?

UraniumAnchor
May 21, 2006

Not a walrus.
It could be, but that really has no bearing here, does it? I'm only using it to modify the memory contents of the bool in question.

The underlying issue here is that both b and !b are non-zero in this situation, which broke some if statements that were expecting that property to hold.

The compiler here is merely flipping the lowest bit, rather than setting a non-zero "bool" to zero. I suspect that's not correct behavior, but I'm not sure. I worked around it anyway because the REAL issue was uninitialized memory, but this particular bit of code doesn't have that problem and still acts in an "unexpected" way.

OddObserver
Apr 3, 2009
I think it's correct behavior, assuming this is C++, and not some typedef.
bool's are permitted to behave as neither true nor false if you don't use
them in a proper, well-defined, typesafe way (the standard gives an example of an uninitialized bool); and this is clearly doing weird stuff since there is nothing that says that true and false have to be represented in a certain way (in particular, sizeof(bool) need not be 1); merely that they convert to and from ints in a certain way.

UraniumAnchor
May 21, 2006

Not a walrus.
Yeah, that's what I suspected but wasn't sure. Thanks.

schnarf
Jun 1, 2002
I WIN.

UraniumAnchor posted:

So I noticed something odd while trying to fix a bug today, using Android NDK r8b (GCC 4.6.x):

code:
bool b;
char *c = (char *)&b;
*c = 152;
while (true) {
	cout << b << endl;
	b = !b;
}
code:
152
153
152
153
152
153
Undefined behavior, or genuine compiler bug?

Undefined behavior -- not only can it behave in the weird way you observed, but it can poison your entire program, and do something completely ridiculous. It's a violation of strict aliasing. This has more information: http://dbp-consulting.com/StrictAliasing.pdf

Edit: I think I'm wrong here, actually. C99 says that any type can be safely accessed through char*, and C++11 says basically the same. I'm not sure what the deal is with bool, though.

schnarf fucked around with this message at 03:58 on Aug 7, 2012

That Turkey Story
Mar 30, 2003

schnarf posted:

Undefined behavior -- not only can it behave in the weird way you observed, but it can poison your entire program, and do something completely ridiculous. It's a violation of strict aliasing. This has more information: http://dbp-consulting.com/StrictAliasing.pdf

Edit: I think I'm wrong here, actually. C99 says that any type can be safely accessed through char*, and C++11 says basically the same. I'm not sure what the deal is with bool, though.

Your initial claim was correct. Yes, you are able to cast and access through char*, but all bets are off once you write via a char and then try to read back as a bool. That is undefined. It is, however, valid to write to the memory via the char*, then write to the memory with the bool, and then read through it via the bool.

shrughes
Oct 11, 2008

(call/cc call/cc)

That Turkey Story posted:

Your initial claim was correct. Yes, you are able to cast and access through char*, but all bets are off once you write via a char and then try to read back as a bool. That is undefined. It is, however, valid to write to the memory via the char*, then write to the memory with the bool, and then read through it via the bool.

What if you write to the memory with the bool, then read with the char*, then write that char value back through the char*, then read with the bool?

pseudorandom name
May 6, 2007

char* is allowed to alias any other type, so that wouldn't be the issue.

It's just the compiler making (perfectly legal) assumptions about the possible values of bool, and only ever looking at the least significant bit as a result.

This is a good example of why you should never use bool (or _Bool, or any kind of enum) in a wire format because the other end can't be trusted.

That Turkey Story
Mar 30, 2003

shrughes posted:

What if you write to the memory with the bool, then read with the char*, then write that char value back through the char*, then read with the bool?

Yeah, that's fine for pods as long as you're copying the same amount of memory (again, a sizeof( bool ) might not be the same as sizeof( char )). I think the actual requirements are more general than that but I'm too lazy to check -- probably just that the type has a trivial copy constructor and a trivial destructor but don't quote me on that. I'm sure someone here knows offhand I.E. rjmccall.

E: or maybe just trivial copy assignment

That Turkey Story fucked around with this message at 05:15 on Aug 7, 2012

megalodong
Mar 11, 2008

I've got barely any knowledge of c++ so this is probably a dumb question, but...

I've been trying to get some linux c++ code to compile on osx under the xcode 4.4 llvm compiler. I've found out how to make it compile, but I'm not sure exactly why it was complaining in the first place.

A stripped-down version of the code:
code:
struct Foo
{
  int x;

  Foo(): x(1){}

  Foo(int i): x(i){}

  Foo(Foo & other) {x = other.x;}
};

int main(int argc, char **argv)
{
  Foo f;
  f = Foo(12);

  return 0;
}
Under linux (and MS VC++ for that matter), the above compiles fine. But under llvm, it complains that there is "no matching function for call to Foo::Foo(Foo)" and lists the non-const copy constructor and single-int-arg constructor as candidates.
If I change the "Foo & other" to be a reference-to-const "Foo const& other", it compiles fine. From what I've read it's best to have this in a copy constructor anyway, but this isn't my code.

But I don't know why the copy constructor matters for the line
code:
f = Foo(12);
in the first place. Like I said, I've never done any c++ before, but I though that line would result in

  • Foo(12) being a functional notation cast, equivalent to something like Foo f(12) with f being an unnamed temporary. So the Foo::Foo(int) constructor is called
  • Foo's copy assignment operator being called to assign that temporary to f. No copy constructor since f is already initialised in the above line.

Could anyone describe exactly what the compiler does then, or some section in the spec that says? Thanks.

tractor fanatic
Sep 9, 2005

Pillbug
Foo(12) creates a temporary Foo and under C++'s rules non const references don't match temporaries. MSVC has an extension to C++ that lets non const references take temporaries but I'm not sure what's going on in linux.

pseudorandom name
May 6, 2007

clang doesn't warn with -std=c++11 and the warning specifically mentions C++98, so maybe the standard changed?

edit: The clang docs for -Wbind-to-temporary-copy specifically says this changed from C++98 to C++11.

Adbot
ADBOT LOVES YOU

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

That Turkey Story posted:

Yeah, that's fine for pods as long as you're copying the same amount of memory (again, a sizeof( bool ) might not be the same as sizeof( char )). I think the actual requirements are more general than that but I'm too lazy to check -- probably just that the type has a trivial copy constructor and a trivial destructor but don't quote me on that. I'm sure someone here knows offhand I.E. rjmccall.

It's [basic.types]p2. You can copy an object into a char array or into another object, but the objects have to be the same (dynamic) type, that type has to be "trivially copyable", and the objects have to be complete objects (i.e. not base class sub-objects). Of course, you also can't violate aliasing rules while you're at it. So you can use a char* to write into a bool object, but if the value you're writing there didn't come from a (validly initialized) bool object, you're breaking the rules.

There's also some language about object vs. value representation, with the intent that you can construct object representations which are not legal value representations, but then using a value loaded from that object has undefined behavior. IIRC, this part is clearer in the C standard.

"Trivially copyable" means (1) trivial destructor, (2) no non-trivial copy or move constructors, and (3) no non-trivial copy or move assignment operators.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply