Saturday, January 17, 2015

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

JiiPee <no@notvalid.com>: Jan 17 12:54PM

So I have this global function which I want to be a friend of a class. I
tried all kind of combinations after googling but none worked so far. If
anybody remembers how to do this would be nice, thanks. Here is the code:
 
// forwards declaring things for the template function - from google...
template <typename T>
class Vector2D;
 
template <typename T>
Vector2D<T> operator * (const T& pointXY, const Vector2D<T>& point);
 
// my class
template <typename T>
class Vector2D : public Coordinate2D<T>
{
public:
// this is what I tried last time.. does not work
friend Vector2D<T> operator * (const T& pointXY, const Vector2D<T>&
point);
};
 
// the global function implementation:
template <typename T>
inline Vector2D<T> operator * (const T& pointXY, const Vector2D<T> & point)
{
return point * pointXY;
}
Victor Bazarov <v.bazarov@comcast.invalid>: Jan 17 08:19AM -0500

On 1/17/2015 7:54 AM, JiiPee wrote:
> {
> return point * pointXY;
> }
 
Please read the FAQ 5.8 _carefully_. You can find the FAQ to this
newsgroup here: http://www.parashift.com/c++-faq-lite/
 
Victor
--
I do not respond to top-posted replies, please don't ask
JiiPee <no@notvalid.com>: Jan 17 01:36PM

On 17/01/2015 13:19, Victor Bazarov wrote:
 
> Please read the FAQ 5.8 _carefully_. You can find the FAQ to this
> newsgroup here: http://www.parashift.com/c++-faq-lite/
 
> Victor
 
I actually did read (quite carefully) that and tried it, but it did not
work. But I now double checked, tried again, and still not working. On
site it says (
http://www.parashift.com/c++-faq-lite/template-friends.html ) :
 
"one simple approach is pre-declare each template friend function /above/"
 
I did that (pls see my code)....
 
Then it says:
"Also you add <> in the friend lines, as shown:
 
friend Foo<T> operator+ <> (const Foo<T>& lhs, const Foo<T>& rhs);
 
"
 
I also did that, then my line becomes:
 
friend Vector2D<T> operator * <> (const T& pointXY, const Vector2D<T>&
point);
 
But if I try to compile this it gives an error: "error: declaration of
'operator*' as non-function|"
 
Why is it not working for me?
Melzzzzz <mel@zzzzz.com>: Jan 17 02:38PM +0100

On Sat, 17 Jan 2015 13:36:56 +0000
 
> But if I try to compile this it gives an error: "error: declaration
> of 'operator*' as non-function|"
 
> Why is it not working for me?
 
Perhaps you should put template keyword?
JiiPee <no@notvalid.com>: Jan 17 01:51PM

On 17/01/2015 13:38, Melzzzzz wrote:
>> of 'operator*' as non-function|"
 
>> Why is it not working for me?
> Perhaps you should put template keyword?
 
I tried all 4 combinations (template, <>) and none worked. Template
before friend and after
 
Template friend
 
friend Template
Louis Krupp <lkrupp@nospam.pssw.com.invalid>: Jan 17 07:03AM -0700


>But if I try to compile this it gives an error: "error: declaration of
>'operator*' as non-function|"
 
>Why is it not working for me?
 
Can you post a complete code sample that reproduces the problem?
 
Louis
Melzzzzz <mel@zzzzz.com>: Jan 17 03:06PM +0100

On Sat, 17 Jan 2015 13:51:52 +0000
> before friend and after
 
> Template friend
 
> friend Template
 
template <> friend Vector2D<T> operator*(...)
JiiPee <no@notvalid.com>: Jan 17 02:10PM

On 17/01/2015 14:06, Melzzzzz wrote:
> template <> friend
 
"error: explicit specialization in non-namespace scope 'class
ct::Vector2D<T>'|"
 
but... the foo-example on the site works... so maybe I have to check
very carefully what is the difference... just cannot find it... somebody
help :)
Victor Bazarov <v.bazarov@comcast.invalid>: Jan 17 09:12AM -0500

On 1/17/2015 9:03 AM, Louis Krupp wrote:
>> 'operator*' as non-function|"
 
>> Why is it not working for me?
 
> Can you post a complete code sample that reproduces the problem?
 
I wonder how long it will take JiiPee to re-read FAQ 5.8 and follow the
instructions.
 
V
--
I do not respond to top-posted replies, please don't ask
Victor Bazarov <v.bazarov@comcast.invalid>: Jan 17 09:13AM -0500

On 1/17/2015 8:51 AM, JiiPee wrote:
> before friend and after
 
> Template friend
 
> friend Template
 
<sigh> I pointed you to the question 5.8 not because I wanted you to
read the template section of the FAQ but because I wanted you to follow
*exactly* the recommendation on how to post questions about the code
that does not work correctly. Did you? Did you post a complete code?
Did you post the compiler output verbatim? ... And all that needs to be
in the same message! It's not enough to *desire* help.
 
Please, post the complete code that we could copy-and-paste into our
development environment to be compiled (or tried to be compiled).
Please post *verbatim* the compiler error you get (we don't all have the
same compiler as you.
 
Keep in mind that templates need to be instantiated (i.e. *used*) to be
analyzed by the compiler and linker.
 
Do you get me?
 
V
--
I do not respond to top-posted replies, please don't ask
JiiPee <no@notvalid.com>: Jan 17 02:14PM

On 17/01/2015 13:19, Victor Bazarov wrote:
 
> Please read the FAQ 5.8 _carefully_. You can find the FAQ to this
> newsgroup here: http://www.parashift.com/c++-faq-lite/
 
> Victor
 
Ok, I compiled the site example, and it works. So obviously in my code
there is something I cannot find. I ll try to double check..
 
site example which works:
 
template<typename T> class Foo; // pre-declare the template class itself
template<typename T> Foo<T> operator+ (const Foo<T>& lhs, const Foo<T>&
rhs);
 
template<typename T>
class Foo {
public:
Foo(T const& value = T());
friend Foo<T> operator+ <> (const Foo<T>& lhs, const Foo<T>& rhs);
private:
T value_;
};
 
template<typename T>
Foo<T>::Foo(T const& value)
: value_(value)
{ }
 
template<typename T>
Foo<T> operator+ (const Foo<T>& lhs, const Foo<T>& rhs)
{ return Foo<T>(lhs.value_ + rhs.value_); }
JiiPee <no@notvalid.com>: Jan 17 02:19PM

On 17/01/2015 14:13, Victor Bazarov wrote:
> read the template section of the FAQ but because I wanted you to
> follow *exactly* the recommendation on how to post questions about the
> code that does not work correctly.
 
oh I see... ok, If the error remains I do that.
 
 
 
> Keep in mind that templates need to be instantiated (i.e. *used*) to
> be analyzed by the compiler and linker.
 
yes i know... i did that.
 
I ll try to find myself the error... if not I post complete code.
"Öö Tiib" <ootiib@hot.ee>: Jan 17 06:31AM -0800

On Saturday, 17 January 2015 15:37:15 UTC+2, JiiPee wrote:
 
> > Victor
 
> I actually did read (quite carefully) that and tried it, but it did not
> work. But I now double checked, tried again, and still not working.
 
Sorry, but then you can't read:
 
5.8.2 Post complete code: put in all necessary #includes and declarations of needed types and functions
5.8.5 Post the tools you used: compiler name, version number, operating system, etc
5.8.6 Post the tool options you used: libraries, exact compiler and linker options, etc
5.8.7 Post the exact messages you received; differentiate between compiler, linker, and runtime messages
 
Instead you paste piece of code that can't compile nowhere and write "it
didn't work, I tried again, it didn't work".
 
To me all compiler's also give errors in the lines of "I have no idea what
is Coordinate2D.", also ... I can not see problems with that friend.
JiiPee <no@notvalid.com>: Jan 17 02:33PM

Ok, here is the error code which can be compiled. I posted the
foo-version from the site which works.. so cannot see what is the
difference:
 
template <typename T> class Vector2D2;
template <typename T> Vector2D2<T> operator * (const T& pointXY, const
Vector2D2<T>& point);
 
template <typename T>
class Vector2D2
{
public:
// error: none of these works...
friend Vector2D2<T> operator* <> (const T& pointXY, const
Vector2D2<T>& point);
// friend Vector2D2<T> operator* (const T& pointXY, const
Vector2D2<T>& point);
T b;
};
 
template <typename T>
Vector2D2<T> operator* (const T& pointXY, const Vector2D2<T>& point)
{
Vector2D2<T> cc;
cc.b = point.b * pointXY;
return cc;
}
 
int main()
{
Vector2D2<double> jj;
3.0 * jj;
}
JiiPee <no@notvalid.com>: Jan 17 02:34PM

On 17/01/2015 14:31, Öö Tiib wrote:
> "it didn't work, I tried again, it didn't work". To me all compiler's
> also give errors in the lines of "I have no idea what is
> Coordinate2D.", also ... I can not see problems with that friend.
 
ok, i just posted a complete code
JiiPee <no@notvalid.com>: Jan 17 02:35PM

On 17/01/2015 14:03, Louis Krupp wrote:
> Can you post a complete code sample that reproduces the problem?
 
> Louis
 
Just posted it.
JiiPee <no@notvalid.com>: Jan 17 03:00PM

hmmm, sometimes my compiler compiles it and sometimes not....
 
I ll try to first sort out where is the real problem. ... I ll come back
if the error remains.
 
 
 
 
On 17/01/2015 14:33, JiiPee wrote:
JiiPee <no@notvalid.com>: Jan 17 03:21PM

it was the inheritance what caused the problem.. so something in base
class. The template works. So if I change the operator from * to % for
example, then everything compiles. So in base class there is also * -
operator which gave troubles .
 
Well, the advice to make a code which you guys can compile it helped to
find this out.
 
Now just have to check the base class why this happens....
JiiPee <no@notvalid.com>: Jan 17 03:18PM

it was the inheritance what caused the problem.. so something in base
class. The template works. So if I change the operator from * to % for
example, then everything compiles. So in base class there is also * -
operator which gave troubles :).
 
Well, the advice to make a code which you guys can compile it helped to
find this out.
 
Now just have to check the base class why this happens....
JiiPee <no@notvalid.com>: Jan 17 03:21PM

On 17/01/2015 15:18, JiiPee wrote:
 
> Well, the advice to make a code which you guys can compile it helped
> to find this out.
 
> Now just have to check the base class why this happens....
 
oh no, this went to wrong place, heh. Did not mean to start a new thread.
agent@drrob1.com: Jan 17 09:39AM -0500

I am learning c++ after many years of modula-2. To my eye, assignment
operator is := and equality comparison is =.
 
In ubuntu 14.04, is there a compiler switch for g++ that flags any
embedded assignment statements. This is to help me when I intend ==
instead of =
 
Thanks
"Öö Tiib" <ootiib@hot.ee>: Jan 17 07:08AM -0800


> In ubuntu 14.04, is there a compiler switch for g++ that flags any
> embedded assignment statements. This is to help me when I intend ==
> instead of =
 
The g++ gives warning [-Wparentheses] about it with compiler
option -Wall.
 
g++:
foo.cpp:6:25: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
 
Also clang does same.
clang:
foo.cpp:6:11: warning: using the result of an assignment as a
condition without parentheses [-Wparentheses]
 
I am not sure if that is what you asked for.
Alain Ketterlin <alain@dpt-info.u-strasbg.fr>: Jan 17 04:12PM +0100


> In ubuntu 14.04, is there a compiler switch for g++ that flags any
> embedded assignment statements. This is to help me when I intend ==
> instead of =
 
The flag is -Wparentheses but you can use -Wall for a common set of
warning flags, and add -Wextra if you want more.
 
-- Alain.
Jorgen Grahn <grahn+nntp@snipabacken.se>: Jan 17 10:00AM

On Thu, 2015-01-15, David Brown wrote:
> but the language is mostly the same - and that is one of its key
> advantages. If I write modern C++, I need to make sure I have a very
> recent compiler that supports the features
 
Surely almost anyone can get at least to the C++98 point today?
I've been on Linux for too long (a decade) but it seems to me the
market for non-Unix embedded environments is shrinking, and that gcc
(including the C++ compiler) is replacing proprietary C compilers
inside that market, too.
 
> code has to have learned the new features. But if I write good, clear,
> modern C code, it will compile on anything, and any C programmer can
> work with it.
 
You're right: /that/ last thing is a reason C isn't unnecessary.
There are plenty of good C programmers around, and you sometimes want
to cooperate with them. No, scratch that: you /frequently/ want to
cooperate with them.
 
At the same time, with those programmers, I cannot help sometimes
thinking "wow, she would get even more stuff done if she spent some
time learning C++!". C++ is, to me, a logical next step. If you're
good with C, you'll be even better with C++, and nothing you've
learned so far is wasted.
 
 
> So while I see that there is steadily more scope for the use of C++ in
> traditional C domains (such as in an increasing proportion of embedded
> development), C is far from "crippled" or "unnecessary".
 
I retract my statement about "unnecessary", but I still maintain the
"crippled" part. It's not derogatory. It's just how I see my own C
programming: I know how I would have written the code in C++, but I
have to translate it to less expressive C code.
 
C can, objectively, be seen as more or less a small subset of C++, and
C++ is readily available for free to almost everybody. In that sense C
is crippled.
 
Of course I have to make my C code more or less idiomatic C -- if I
tried to emulate C++ in C I would be as rude as the C programmers
writing C++ code as if it was C -- but I cannot ignore the lessons C++
taught me, about the merits of type safety and so on.
 
And I cannot pretend, when I'm writing yet another linked-list
implementation, that I'm not wasting my hours on this earth on
something that's strictly not necessary.
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
Martijn Lievaart <m@rtij.nl.invlalid>: Jan 17 01:15PM +0100

On Thu, 15 Jan 2015 23:16:49 +0000, Richard wrote:
 
>>arguments on C projects. That mentality transcends language boundaries.
 
> And the whole thing is silly unless you have performance measurements to
> back up your assertions. Otherwise, it's all just wishful thinking.
 
Yes, the three laws of optimization: Profile, profile, profile.
 
I currently work with SO-DIMM sized embedded computers. The things are so
damned powerful they run Debian and I do work in Perl and shell. No need
to shave of another tenth of a millisecond if my application is fine with
response times around a second and I get millisecond response times
anyway.
 
If performance matters for your application you should know why and
hopefully are able to measure it.
 
There is a case where efficiency matters and you may not even be able to
measure it (meaningful, easily). That is when writing general stuff.
OSses, general libraries and interpreted languages f.i.
 
That sucks, but it's still possible to get some kind of measurements to
make some statements about efficiency. See for instance how the Perl and
Linux communities (and probably dozens of others) handle this.
 
In any case, anecdotal evidence is the worst kind of optimization
technique. It just does not work. It introduces all kinds of all new hard
to find bugs while probably only slowing things down and defeating
maintainability.
 
There is only one good way to do good optimization. Understand what you
are doing and measure to tell if you are correct and it really works. If
that is impossible, you may fall back to you don't know what you are
doing, but will gain insight through measurements.
 
[snip]
 
> that is their simplistic takeway from the real advice of "keep your
> cache hot". The latter is what you need to remember, not simplistic
> bugaboos about particular language features.
 
I'll take your word for it, as you seem to know what you are talking
about (and keep your caches hot is on the micro level the most important
optimization). However, in this particular instance, I remember that some
older (extinct) microprocessors had rather slow indirect function calls,
so that may also spark those 'no virtual functions' rants. In the end,
profile, profile, profile.
 
M4
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: