C vs. C++

J

jacob navia

Tomás Ó hÉilidhe said:
C++ is C with more features added, and is inherently better.

This is wrong.

For instance

EXHIBIT 1
---------

template<class T, int ndims>
class NFieldBase {
public: static const T max;
};

template<int ndims> const float NFieldBase<float, ndims>::max = 1.0f;

When compiled with g++, I get this error:

test.cpp:7: error: template definition of non-template ‘const float
NFieldBase<float, ndims>::max’


(1) Why do I get this error?

(2) How do I fix it?


You see?

More is less. More complexity is less usability. More features make
complexity grow, and it is non linear. If you add <n> features, you
get <n squared> complexity increase.
 
R

REH

For example, a given C function might be declared either as
    double foo(double x, double y);
or as
    double foo();
The latter is illegal in C++, if I recall correctly.  In C, a pointer
of type ``double (*p)()'' can point to any non-variadic function.

It's legal in C++, but it means the same as
double foo(void);
To get the C behavior, you would have to declare it thus:
double foo(...);


REH
 
K

Keith Thompson

REH said:
It's legal in C++, but it means the same as
double foo(void);

You're right.
To get the C behavior, you would have to declare it thus:
double foo(...);

Not quite. C's
double foo();
declares foo as a function returning double and taking an unspecified
*but fixed* number and type(s) of arguments. C++ has no equivalent,
as far as I know.
double foo(...);
is actually illegal, but
double foo(some_type first_arg, ...);
declared foo as a variadic function, which is incompatible with
"double foo()".
 
R

REH

This is wrong.

For instance

EXHIBIT 1
---------

template<class T, int ndims>
class NFieldBase {
    public: static const T max;

};

template<int ndims> const float NFieldBase<float, ndims>::max = 1.0f;

When compiled with g++, I get this error:

test.cpp:7: error: template definition of non-template ‘const float
NFieldBase<float, ndims>::max’

(1) Why do I get this error?

(2) How do I fix it?

You see?

More is less. More complexity is less usability. More features make
complexity grow, and it is non linear. If you add <n> features, you
get <n squared> complexity increase.

I don't understand. You write syntactically incorrect code, and it is
the language's fault?

REH
 
R

REH

You're right.


Not quite.  C's
    double foo();
declares foo as a function returning double and taking an unspecified
*but fixed* number and type(s) of arguments.  C++ has no equivalent,
as far as I know.
    double foo(...);
is actually illegal, but
    double foo(some_type first_arg, ...);
declared foo as a variadic function, which is incompatible with
"double foo()".
No, it's not illegal. In C++, it mean the same as I explained. It is
also used to define a function that is the worst match for overload
resolution.

REH
 
J

jacob navia

REH said:
I don't understand. You write syntactically incorrect code, and it is
the language's fault?

You are wrong, this is not a syntax error.

Victor Bazarov, one C++ expert said about it:

<quote>
So, you're trying to define the member of a specialisation without
actually defining the specialisation itself, right? I am not sure it's
allowed.
<end quote>

He is not sure if this is allowed or not.

Another expert (Jeff Schwab) said:
<quote>
It's allowed for member functions, and I believe for member types. It
sounds from the OP like this is only an error for partial
specializations. Strange.
<end quote>

This people have been programming in C++ for YEARS, and (specially
Bazarov) are recognized experts in C++.

And they can't figure out what is this problem. Nice. Can
YOU figure it out?

This is no syntax error my dear.
 
R

REH

You are wrong, this is not a syntax error.

Victor Bazarov, one C++ expert said about it:

<quote>
So, you're trying to define the member of a specialisation without
actually defining the specialisation itself, right?  I am not sure it's
allowed.
<end quote>

He is not sure if this is allowed or not.

Another expert (Jeff Schwab) said:
<quote>
It's allowed for member functions, and I believe for member types.  It
sounds from the OP like this is only an error for partial
specializations.  Strange.
<end quote>

This people have been programming in C++ for YEARS, and (specially
Bazarov) are recognized experts in C++.

And they can't figure out what is this problem. Nice. Can
YOU figure it out?

This is no syntax error my dear.
Yes, Victor is an acknowledged expect. He was gracious enough to peer
review a C++ article I published in Dr. Dobb's some time ago. But
repectfully disagree. This don't look valid to me.

REH
 
R

REH

You are wrong, this is not a syntax error.

Victor Bazarov, one C++ expert said about it:

<quote>
So, you're trying to define the member of a specialisation without
actually defining the specialisation itself, right?  I am not sure it's
allowed.
<end quote>

He is not sure if this is allowed or not.

Another expert (Jeff Schwab) said:
<quote>
It's allowed for member functions, and I believe for member types.  It
sounds from the OP like this is only an error for partial
specializations.  Strange.
<end quote>

This people have been programming in C++ for YEARS, and (specially
Bazarov) are recognized experts in C++.

And they can't figure out what is this problem. Nice. Can
YOU figure it out?

This is no syntax error my dear.

With G++, I get:
t.cpp:7: error: invalid use of undefined type `class NFieldBase<float,
ndims>'
t.cpp:2: error: declaration of `class NFieldBase<float, ndims>'

with MSVC, I get:
t.cpp(7) : error C3860: template argument list following class
template name must list parameters i
the order used in template parameter list
t.cpp(7) : error C3855: 'NFieldBase': template parameter 'T' is
incompatible with the declaration
t.cpp(7) : error C2976: 'NFieldBase' : too few template arguments
t.cpp(5) : see declaration of 'NFieldBase'

with Comeau, I get:
"ComeauTest.c", line 7: error: template argument list must match the
parameter list
template<int ndims> const float NFieldBase<float, ndims>::max =
1.0f;
^

1 error detected in the compilation of "ComeauTest.c".
 
J

jacob navia

REH said:
Yes, Victor is an acknowledged expect. He was gracious enough to peer
review a C++ article I published in Dr. Dobb's some time ago. But
repectfully disagree. This don't look valid to me.

"Doesn't look valid"...

Why? Can you explain?

No, you can't, and Mr Bazarov could not explain either. The language
has grown to such monstrous complexity that not everyone can see
what is wrong with some complex constructs, even if the code is
just a few lines long.

I do not want to say "C++ is a pile of sh...". C++ is a big
language, and there are *many* good ideas in C++.

My opinion is, with all respect to you and the C++ people,
that a simpler language is better in the sense that it is
more TRANSPARENT: you can read what it says and understand
fairly quickly what it does.

C has the huge advantage of its simplicity.
 
R

REH

"Doesn't look valid"...

Why? Can you explain?

No, you can't, and Mr Bazarov could not explain either. The language
has grown to such monstrous complexity that not everyone can see
what is wrong with some complex constructs, even if the code is
just a few lines long.

I do not want to say "C++ is a pile of sh...".  C++ is a big
language, and there are *many* good ideas in C++.

My opinion is, with all respect to you and the C++ people,
that a simpler language is better in the sense that it is
more TRANSPARENT: you can read what it says and understand
fairly quickly what it does.

C has the huge advantage of its simplicity.

Why the hostility? If you dislike the language, that's fine. We all
have our favorites.

Yes, I can explain it, but I its obvious to me that however I try to
explain will just be met with derision. So, I will just say "good
day."

REH
 
K

Keith Thompson

REH said:
No, it's not illegal. In C++, it mean the same as I explained. It is
also used to define a function that is the worst match for overload
resolution.

I'm unable to confirm this by reading my copy of (a draft of) the C++
standard. But this is off-topic anyway.
 
J

jacob navia

REH said:
With G++, I get:
t.cpp:7: error: invalid use of undefined type `class NFieldBase<float,
ndims>'
t.cpp:2: error: declaration of `class NFieldBase<float, ndims>'

with MSVC, I get:
t.cpp(7) : error C3860: template argument list following class
template name must list parameters i
the order used in template parameter list
t.cpp(7) : error C3855: 'NFieldBase': template parameter 'T' is
incompatible with the declaration
t.cpp(7) : error C2976: 'NFieldBase' : too few template arguments
t.cpp(5) : see declaration of 'NFieldBase'

with Comeau, I get:
"ComeauTest.c", line 7: error: template argument list must match the
parameter list
template<int ndims> const float NFieldBase<float, ndims>::max =
1.0f;
^

1 error detected in the compilation of "ComeauTest.c".

Good. Note this:

The original error with g++ was:

test.cpp:7: error: template definition of non-template ‘const float
NFieldBase<float, ndims>::max’

Apparently you are using a different version of g++.

Then, we notice that all three compilers give different errors.
MSVC complains that the "float" parameter to the template is
"incompatible with the declaration", and in the declaration of
NFieldBase we see that T is a "class". Should we change "class" to
"typename" in the parameter list?

Etc, etc.

Actually, the work around proposed was to make a "traits class"
and eliminate the partial specialization.

For a commetary see the other post I sent in this same thread.
 
J

jacob navia

REH said:
Why the hostility? If you dislike the language, that's fine. We all
have our favorites.

Excuse me but I said "with all respect to you and the C++ people".
I do not have any hostility against you or against C++. I have a
critique: too complex.

Why can't we discuss without any emotional tone?

I do not want to say that C++ is "bad" or similar. I think
however that it has grown without bounds, grown so much
that it has grown beyond what a normal programmer's mind
can understand in a lifetime.

So, everyone uses subsets of C++ since the language is so
big. And if you stay in your C++ subset things tend to
work.

Again, no offense intended.
 
C

CBFalconer

^^^^^^^^^^^^^^^^^^
There is /nothing/ that can be achieved more elegantly or more
efficiently in C, than it can be achieved in C++. Nothing. If
you think there's something then I'll listen intently.

etc., for a particular peculiar definition of 'better'. However, I
am largely writing this to remind you of proper practice. I did
NOT write the part you quoted and that is underlined. I did create
attributions for all quoted material. Please do not strip
attributions for material you quote, even if it is your own. If
you modify a quote, add something that identifies your modification
and the fact that you did it. This is not hard, with any half
decent news reader.
 
A

Antoninus Twink

Tomás Ó hÉilidhe said:
There is /nothing/ that can be achieved more elegantly or more
efficiently in C, than it can be achieved in C++. Nothing. If
you think there's something then I'll listen intently.

However, I am largely writing this to remind you of proper practice.
[tedious net nannying snipped]

Above all, make sure you don't address the substantive point.
 
R

REH

Excuse me but I said "with all respect to you and the C++ people".
I do not have any hostility against you or against C++. I have a
critique: too complex.

Why can't we discuss without any emotional tone?

I do not want to say that C++ is "bad" or similar. I think
however that it has grown without bounds, grown so much
that it has grown beyond what a normal programmer's mind
can understand in a lifetime.

So, everyone uses subsets of C++ since the language is so
big. And if you stay in your C++ subset things tend to
work.

Again, no offense intended.

If I understand what you are trying to do, you are trying to define
the max member for the template when T is a float. You are simply
trying to use a partial specialization of the template, but you
haven't declared it. This is not allowed in C++. You don't need a
workaround, nor a "traits" class. You simply need to declare the
specialization:

template<class T, int ndims>
class NFieldBase {
public: static const T max;

};

template<int ndims>
class NFieldBase<float, ndims> {
public: static const float max;

};

template<int ndims> const float NFieldBase<float, ndims>::max = 1.0f;


The above compiles fine.
REH
 
J

jameskuyper

REH wrote:
....
I don't understand. You write syntactically incorrect code, and it is
the language's fault?

It could be, if his syntax error occurred because the language's
syntax rules are too complex or counterintuitive. However, he'll need
to actually present an argument that this is in fact the case. Merely
presenting examples of mistakes he's made without any accompanying
argument doesn't prove anything.
 
C

CBFalconer

jacob said:
.... snip ...

My opinion is, with all respect to you and the C++ people,
that a simpler language is better in the sense that it is more
TRANSPARENT: you can read what it says and understand fairly
quickly what it does.

C has the huge advantage of its simplicity.

And how it does it. I and many others here, wholeheartedly agree.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,200
Latest member
LaraHunley

Latest Threads

Top