Monday, May 4, 2015

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

Doug Mika <dougmmika@gmail.com>: May 04 01:58PM -0700

I have the following short ReverseSort algorithm implemented in a structs operator() method:
 
template<typename KeyType>
struct ReverseSort{
bool operator()(const KeyType& key1, const KeyType& key2){
return (key1 > key2);
}
};
 
My Question is why, when declaring a map I write
 
map<int, string, ReverseSort<int> > someOtherMap (someOtherMap.cbegin(), someOtherMap.cend());
 
and NOT
 
map<int, string, ReverseSort<int>() > someOtherMap (someOtherMap.cbegin(), someOtherMap.cend());
 
[Note the brackets () in ReverseSort<int>()]
Afterall the decleration of map states that it takes a binary predicate, then why isn't it an object of a functor (ReverseSort<int>()) instead of just a type (ReverseSort<int>)??
Doug Mika <dougmmika@gmail.com>: May 04 03:40PM -0700

With a follow up question to the above... why does the following not work
 
#include <string>
#include <map>
 
using namespace std;
 
template<typename KeyType>
bool ReverseSort(const KeyType& key1, const KeyType& key2){
return (key1 > key2);
}
 
 
 
int main(int argc, char** argv) {
 
// Prints arguments...
std::cout<<"Command Parameters: "<<std::endl;
if (argc > 1) {
std::cout << std::endl << "Arguments:" << std::endl;
for (int i = 1; i < argc; i++) {
std::cout << i << ": " << argv[i] << std::endl;
}
}

//The Program
map<int, string> mapIntToString1;

map<int, string, ReverseSort<int> > mapIntToString2 (mapIntToString1.cbegin(), mapIntToString1.cend());
}
 
The problem is with my template function ReverseSort<int>, but why can't I include it as the binary predicate in map?
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: May 04 08:26PM +0100

On 04/05/2015 09:00, Juha Nieminen wrote:
 
> And yes, I need to use std::int32_t & co because this is rather low-level
> binary code manipulation which needs to handle exact bit sizes.
 
> --- news://freenews.netfront.net/ - complaints: news@netfront.net ---
 
You shouldn't be mixing 'long' with the sized integer typedefs in fact
you shouldn't be using 'long' (or even 'int') at all: always use the
sized integer typedefs.
 
/Flibble
Vir Campestris <vir.campestris@invalid.invalid>: May 04 09:06PM +0100

On 04/05/2015 18:24, Richard wrote:
 
> If it does, then use long long's constants for 64-bit integers,
> alternatively you can explicitly select the overload by doing:
 
> foo(static_cast<std::int64_t>(123));
 
I don't see why this would make it compile OK giving it 123, but not
123L. (you snipped "Note that if I call foo(123) it does not
give any error.")
 
Andy
Paavo Helde <myfirstname@osa.pri.ee>: May 04 03:46PM -0500

Juha Nieminen <nospam@thanks.invalid> wrote in news:mi78vh$2i9c$1
 
> void foo(long long) { std::cout << "long long\n"; }
 
> it gives me:
 
> test.cc:7:6: error: redefinition of 'foo'
 
This clearly shows that your implementation has typedeffed int64_t as
long long, not as long. The standard says int64_t must be a typedef, but
does not say of which type. And it cannot be a typedef of long and long
long at the same time - these are required to be different types.
 
Cheers
Paavo
Jorgen Grahn <grahn+nntp@snipabacken.se>: May 04 10:04PM

On Mon, 2015-05-04, L?rinczy Zsigmond wrote:
>> void foo(std::int64_t) { std::cout<< "int64_t\n"; }
 
> Overloading is inherently problematic, it is unavoidable.
> Have one function with intmax_t type parameter.
 
It's problematic with the builtin types and all their implicit
conversions (like here), but not in general.
 
/Jorgen
 
--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
Dombo <dombo@disposable.invalid>: May 04 09:04PM +0200

Op 04-May-15 14:59, Joseph Hesse schreef:
> of City occurs after the declaration of Person?
 
> Thank you,
> Joe
 
 
You need a forward declaration of City so the compiler knows that "City"
is a class:
 
class City;
 
class Person
{
private:
City *city; // the city in which the person lives
// ...
public:
// ...
};
 
class City
{
private:
std::set<Person> citizens;
// ...
public:
// ...
};
 
Note that a forward declaration suffices for the Person class because
the city member is a pointer. If the city member was an instance of the
class City it wouldn't compile because at that point the compiler
doesn't know yet how big instances of the City class will be and
therefore cannot know how big instances of the Person class would be.
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: