|
![](/i/fill.gif) |
Le 28/02/2014 09:28, Orchid Win7 v1 a écrit :
>>> Not that VS is perfect, of course. Yesterday it gave me the dreaded
>>> C2512 error: "no appropriate default constructor available".
>>
>> Again, I'd have to see an actual example...
>
> struct Side
> {
> Pattern & Pattern;
> Pattern::iterator & Iterator;
> }
>
> struct State
> {
> Side & LeftSide;
> Side & RightSide;
> std::stringstream Buffer;
> bool Success;
> }
>
> Every single line that declares a State variable gives me C2512. As you
> can see, I clearly *have not* defined any constructors, so that isn't
> the reason for a default constructor not existing.
But the compiler is unable to generate the default constructor (no
parameter) when some fields are references: they cannot be nullptr, and
they cannot be changed after the constructor, so what would be their
default value ?
By using reference as fields, you disabled the default implicit
constructor. It's your responsibility to now provides one, if you want it.
> I didn't mean references specifically. I just meant the entire language.
> Nobody would try to claim that C++ is small or simple or free of
> surprises. For example,
>
> struct Side
> {
> Pattern & ThePattern;
> Side(Pattern p) : ThePattern(p) {}
> }
>
> Guess what? This segfaults when I run it. *You* probably see the problem
> immediately, but it took me 24 hours to figure out that there's a single
> missing character there...
two missing characters ? ;& ?
the ; after the last }.
the & in the constructor (Side(Pattern& p) ).
>
> Today it's references. Last time it was
>
> catch (std::exception e)
> {
> std::cout << e.what();
> }
>
> which *always* says std::exception, even if that wasn't the exception
> thrown. The time before that, it was a memory leak due to an (empty)
> destructor not being virtual. There's so many buts and gotchas. And
> they're not the sort of thing where your program gives you the wrong
> answer; they're the sort of thing where you have a native crash and
> you're left utterly bewildered as to how the *hell* to discover what the
> problem was...
catch should use reference & const, to avoid copy constructor with
implicit upper-casting (that's the reason of the reference) and save
some time on exit of the block (that's the const).
catch(const std::exception & e)
--
Just because nobody complains does not mean all parachutes are perfect.
Post a reply to this message
|
![](/i/fill.gif) |