Sunday, May 31, 2020

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

Juha Nieminen <nospam@thanks.invalid>: May 31 04:13PM

It's still unclear to me what the difference is between the function type
syntax of the form "ReturnType(*)(ParameterTypes)" and
"ReturnType(ParameterTypes)".
 
Sometimes they seem to be interchangeable, sometimes they don't.
 
For example, this is valid:
 
//-------------------------------------------
using F1 = int(*)(int);
using F2 = int(int);
 
void foo1(F1 funcPtr); // ok
void foo2(F2 funcPtr); // ok
//-------------------------------------------
 
However, this is not valid:
 
//-------------------------------------------
F1 funcPtr1 = someFunc; // ok
F2 funcPtr2 = someFunc; // error
//-------------------------------------------
 
Likewise:
 
//-------------------------------------------
std::function<int(int)> funcObj1; // ok
std::function<int(*)(int)> funcObj2; // error
 
std::set<int, bool(*)(int, int)> set1; // ok
std::set<int, bool(int, int)> set2; // error
//-------------------------------------------
 
So they are like the reverse of each other.
 
F2 above can be used for a function declaration, which might give some
insight into what it means. In other words:
 
//-------------------------------------------
F2 someFunction; // ok
 
int main()
{
int value = someFunction(5); // ok.
}
 
int someFunction(int v) { return v+1; }
//-------------------------------------------
Ben Bacarisse <ben.usenet@bsb.me.uk>: May 31 05:49PM +0100


> It's still unclear to me what the difference is between the function type
> syntax of the form "ReturnType(*)(ParameterTypes)" and
> "ReturnType(ParameterTypes)".
 
The first is a pointer type and the second a function type.
 
> using F2 = int(int);
 
> void foo1(F1 funcPtr); // ok
> void foo2(F2 funcPtr); // ok
 
Because functions can't be passed as parameters, function types are
converted to pointer-to-function types in this context. This is
annoying, but it's hang-over from C.
 
It's analogous to (raw) array types in parameter lists -- they also get
converted to pointer types.
 
> F1 funcPtr1 = someFunc; // ok
> F2 funcPtr2 = someFunc; // error
> //-------------------------------------------
 
And you can't assign to a function.
 
 
> //-------------------------------------------
> std::function<int(int)> funcObj1; // ok
> std::function<int(*)(int)> funcObj2; // error
 
You need a function type, not a pointer type, for std::function.
 
> std::set<int, bool(int, int)> set2; // error
> //-------------------------------------------
 
> So they are like the reverse of each other.
 
A set hold data objects, and pointers are data objects so a set of
pointers makes sense. Functions are not considered to be data objects
in either C or C++ so you can't put them into collections.
 
> insight into what it means. In other words:
 
> //-------------------------------------------
> F2 someFunction; // ok
 
Yes, F2 is a function type and can be used to declare (but not define)
functions.
 
 
--
Ben.
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: May 27 12:26PM +0200

On 27.05.2020 01:52, Chris M. Thomasson wrote:
 
> Basically. However, I want to extend this to handle higher bases. Right
> how its only a 2-ary tree. It can be extended to n-ary. The code is down
> right crude and very basic for now.
 
Oh look, an efficient integral power function:
 
 
namespace impl
{
constexpr inline auto intpow( const double base, const int
exponent )
-> double
{
double result = 1;
double weight = base;
for( int n = exponent; n != 0; weight *= weight ) {
if( is_odd( n ) ) {
result *= weight;
}
n /= 2;
}
return result;
}
} // namespace impl
/// @endcond
 
/// \brief Efficient *x* to the *n*'th power, when *n* is an integer.
///
/// \param base The *x* in "*x* to the *n*'th".
/// \param exponent The *n* in "*x* to the *n*'th".
///
/// Essentially this is Horner's rule adapted to calculating a
power, so that the
/// number of floating point multiplications is at worst O(log2(n)).
constexpr inline auto intpow( const double base, const int exponent )
-> double
{
return (0?0
: exponent == 0? 1.0
: exponent < 0? 1.0/impl::intpow( base, -exponent )
: impl::intpow( base, exponent )
);
}
 
 
<url:
https://github.com/alf-p-steinbach/cppx-core-language/blob/master/source/cppx-core-language/calc/floating-point-operations.hpp>
 
 
[snip]
 
 
- Alf
Juha Nieminen <nospam@thanks.invalid>: May 27 06:35AM

> [Jesus Loves You] Messages of hope and a future
 
You may get a sense of martyrdom and righteous victimhood when you keep
spamming this newsgroup with provocative off-topic content and getting
negative reactions. However, even if your god were real and exactly
like you believe him to be, even he wouldn't approve of you behaving
like this. I think that even he would rebuke you for this kind of
disruptive behavior. He would not be pleased with you. I don't think
even he would want you to spread his word in this manner.
 
The fact that you don't accept that makes you an obsessed bigot.
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: