I
Ike Naar
Le 24/02/2014 16:43, Wouter van Ooijen a ?crit :
actually
c = strcatMany(a,b,NULL);
What's the type of c in the line above?
(Note that strcatMany returns an int).
Le 24/02/2014 16:43, Wouter van Ooijen a ?crit :
actually
c = strcatMany(a,b,NULL);
Scott said:See, that's the C++ programmer view. A real programmer will use ...
... snprintf when and where it makes sense ...
... and not worry about "encapsulating" it ...
... and doesn't care about spurious "language purity".
examples from C++ operating system:
Letting the compiler check arguments, even on user-defined functions
is trivial with a good compiler:
Gerhard Fiedler said:Try letting the compiler check the argument types when using a variable
as formatting string.
I've never seen a case where it makes sense to use a variable as a
formatting string[*]. Can you describe a case where it does make sense
and it doesn't cause a potential security problem?
[*] absent a wrapper function that uses __attribute__((printf(x,y))), anyway.
Le 25/02/2014 00:39, Dombo a écrit :
As you know the equality operator is "==" and NOT "=" that means store
the right hand side in the left hand side...
The discussion is about overusing overloading for output, and I think
most participants agree that it is a PITA.
Now, could a "printf" based
approach: a (mini) "language" and a run time interpreter/formatter be
better suited to output than an overloaded operator?
That is my point.
Scott said:Gerhard Fiedler said:Try letting the compiler check the argument types when using a variable
as formatting string.
I've never seen a case where it makes sense to use a variable as a
formatting string[*]. Can you describe a case where it does make sense
and it doesn't cause a potential security problem?
[*] absent a wrapper function that uses __attribute__((printf(x,y))), anyway.
Scott said:Gerhard Fiedler said:Try letting the compiler check the argument types when using a variable
as formatting string.
I've never seen a case where it makes sense to use a variable as a
formatting string[*]. Can you describe a case where it does make sense
and it doesn't cause a potential security problem?
Simple internationalisation. I've worked on embedded projects where we
used different sets of format strings for different languages. No
security risks there.
[*] absent a wrapper function that uses __attribute__((printf(x,y))),
anyway.
Where is that defined in the C++ standard?
David said:Scott said:Try letting the compiler check the argument types when using a variable
as formatting string.
I've never seen a case where it makes sense to use a variable as a
formatting string[*]. Can you describe a case where it does make sense
and it doesn't cause a potential security problem?
Simple internationalisation. I've worked on embedded projects where we
used different sets of format strings for different languages. No
security risks there.
That would be my thought. You might have something like this:
typedef enum { formatUK, formatUS } dateFormatChoices;
static const char dateFormatStrings[] = {
"%1$02d/%2$02d/%3$04d", "%2$02d/%1$02d/%3$04d"
}
void printDate(dateFormatChoices i, int day, int month, int year) {
printf(dateFormatStrings, day, month, year);
}
(That particular example relies on posix printf with parameters - if you
can't re-arrange parameter order like that, variable format strings are,
I think, significantly less useful.)
David said:Scott Lurndal wrote:
Try letting the compiler check the argument types when using a
variable
as formatting string.
I've never seen a case where it makes sense to use a variable as a
formatting string[*]. Can you describe a case where it does make
sense
and it doesn't cause a potential security problem?
Simple internationalisation. I've worked on embedded projects where we
used different sets of format strings for different languages. No
security risks there.
That would be my thought. You might have something like this:
typedef enum { formatUK, formatUS } dateFormatChoices;
static const char dateFormatStrings[] = {
"%1$02d/%2$02d/%3$04d", "%2$02d/%1$02d/%3$04d"
}
void printDate(dateFormatChoices i, int day, int month, int year) {
printf(dateFormatStrings, day, month, year);
}
(That particular example relies on posix printf with parameters - if you
can't re-arrange parameter order like that, variable format strings are,
I think, significantly less useful.)
The case I was thinking of was a monitoring device that displayed the
name of a unit (such as "Battery Time Remaining") or menu items in the
language selected by the user. Nothing fancy.
Scott said:Try letting the compiler check the argument types when using a variable
as formatting string.
I've never seen a case where it makes sense to use a variable as a
formatting string[*]. Can you describe a case where it does make sense
and it doesn't cause a potential security problem?
Simple internationalisation. I've worked on embedded projects where we
used different sets of format strings for different languages. No
security risks there.
Unfortunately the strictly positional nature of the input parameters
in the printf functions has always led us to use our own replacement.
Scott said:Gerhard Fiedler said:Try letting the compiler check the argument types when using a
variable as formatting string.
I've never seen a case where it makes sense to use a variable as a
formatting string[*]. Can you describe a case where it does make
sense and it doesn't cause a potential security problem?
Because there is no ADDITION being done when a and b are STRINGS but a
concatenation of two strings, what is *completely different*. If a and
b are strings we have
a+b != b+a
what goes against all the mathematical basis of addition as normal
people use that word.
That is why string "addition" is an ABUSE of overloading.
In the same vein, shifting left an object to print it just because the
opeartor << looks like the head of a left arrow or suggests "put into"
is NONSENSE and an abuse of the overloading concept.
jacob navia schreef op 24-Feb-14 3:34 PM:
In true C++: if that is what you want it is cetainly possible, just like
you can have print_xxx functions if you want to.
I think I would prefer to reserve operator<<(person) for (debug)
printing of ALL data in person, and have separate getters for the items
that you might want to print, like
std::cout
<< std::setw( 30 ) << person.name()
<< " lives at "
<< std::setw( 60 ) << person.adress();
This keeps *what* you want to print separate from *how* you want to
print it.
I think what jacob navia is after would be better done as a bunch of
classes which only know how to format a Customer, each in its unique
fashion:
std::cout << ForMailingLabel(some_customer);
std::cout << ForGreetingMessage(some_customer);
std::cout << ForTheNSA(some_customer);
(Sorry about the bad naming; I don't deal with customers so I don't
know what you want to print or why.)
/Jorgen
[snip]I think what jacob navia is after would be better done as a bunch of
classes which only know how to format a Customer, each in its unique
fashion:
std::cout << ForMailingLabel(some_customer);
std::cout << ForGreetingMessage(some_customer);
std::cout << ForTheNSA(some_customer);
Exactly, and I like specially the last one:
std::cout << ForTheNSA(some_customer);
!!!
[snip]I think what jacob navia is after would be better done as a bunch of
classes which only know how to format a Customer, each in its unique
fashion:
std::cout << ForMailingLabel(some_customer);
std::cout << ForGreetingMessage(some_customer);
std::cout << ForTheNSA(some_customer);
Exactly, and I like specially the last one:
std::cout << ForTheNSA(some_customer);
!!!
+1.
If the NSA was really that good, they should slip it into the C++
standard and make it a required method for each class ;-)
"Seldom needs" is such a tired argument. I still remember when it was
used to defend Windows3 as "better" than actual multi-tasking OS's.
part. I distrust strategies which depend on getting things right
in an initial design.
I totally agree. Quality is mostly the result of a long struggle,
feeding new insights and experiences back into the design. Rembember,
99% transpiration.
But not of safety, extensibility or absraction.
Oh, I could write an entire book with all the problems that C has.
Don't even get me started.
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.