Discussion:
C++11 function late return value syntax.
David Oswald
2012-09-17 10:36:48 UTC
Permalink
Traditionally (C++08 or earlier, as well as C), function headers
looked like this:

Type function ( Type param, ... );

C++11 introduced a new (optional) syntax extension:

auto function ( Type param, ... ) -> Type;

It was introduced to make template programming easier, as well as to
make lambda functions (also new in C++11) work better.

Inline::CPP doesn't parse function/method bodies, so the syntax's use
in lambda functions presents no problem. Nor is it a problem for
private methods that aren't intended to bind to Perl. However, if a
user has code that uses this syntax for a function header that needs
to bind to Perl, that user will be out of luck.

int add ( int x, int y ) { return x + y; } // OK.
auto add ( int x, int y ) -> int { return x + y; } // Not ok: Won't
bind to Perl.

This probably isn't a big deal for now; many compilers require flags
to be passed to the compiler even to enable C++11 features, and it's
unlikely to turn up in Inline::CPP-targeted code right away. But for
the future it would be nice to permit the syntax.

Surprisingly, this is just about the only new syntax I've found
(outside of the world of templates) that is problematic for
Inline::CPP. I fully expected the "enum class Type {...};" syntax to
be a problem, but enums don't bind to Perl anyway, so they're just
passed straight through to the C++ compiler.

Is there anyone out there comfortable enough with the
Parse::RecDescent grammar for Inline::CPP to take a stab at the late
return type specification syntax?

I'm happy to add unit tests for the new syntax, and apply patches that
pass without breaking any existing tests. If anyone wants to tinker
with it, fork the Github repo, and issue a pull request if you're
successful.

Dave
--
David Oswald
daoswald-***@public.gmane.org
David Oswald
2012-09-17 17:38:09 UTC
Permalink
Dave,
Are C++ inline functions (not intended for perl binding) OK ?
Are there any problems with malloc ( ) calls being used within C++ code ?
Let's talk about malloc first:

Are you writing C++ code that uses malloc, or are you using
Inline::CPP to handle C code?

If the latter, you can eliminate some complexity by working directly
with Inline::C, but should be using the Perl memory allocation macros
"Newx", "Newxc", or "Newxz" (see perlguts).

If you're actually writing C++ code, you should probably be using
C++'s "new". 'new' is the C++ way of allocating dynamic memory. The
advantages to using it when writing true C++ code are numerous. One
advantage is that 'new' ensures that your object's constructor gets
called (malloc doesn't handle this for you). New is also type-safe
(malloc isn't). And it can be overloaded for non-trivial classes
(malloc can't in any useful way). Similarly, 'delete' (as opposed to
free) assures that destructors are called, and in the proper order.


As for inline functions; they are perfectly legal in Inline::CPP
whether or not they're intended for Perl binding. There's even a set
of tests in grammar/t/03inline.t that verify they work as they should.
The 'inline' keyword in specific will be honored to the same degree
that your compiler honors it (the compiler makes no guarantees; it
simply takes the request into consideration, and then does what it
wants).
--
David Oswald
daoswald-***@public.gmane.org
Continue reading on narkive:
Loading...