Skip to content

Instantly share code, notes, and snippets.

@neomantra
Last active December 11, 2015 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neomantra/4532928 to your computer and use it in GitHub Desktop.
Save neomantra/4532928 to your computer and use it in GitHub Desktop.
#include <cstdint>
#include <iostream>
#include <unordered_map>
struct Foo
{
int a;
};
uint64_t some_func();
int main( int argv, const char* argc[] )
{
std::unordered_map<uint32_t, uint32_t> da_map;
da_map.insert( std::make_pair( 123ULL, uint64_t(-1) ) );
da_map.insert( std::make_pair( some_func(), some_func() ) );
da_map.insert( std::pair<uint32_t, uint64_t>( some_func(), some_func() ) );
std::unordered_map<uint32_t, uint32_t>::iterator iter1 = da_map.find( uint64_t(123ULL) );
std::unordered_map<uint32_t, uint32_t>::iterator iter2 = da_map.find( uint64_t(-1) );
std::unordered_map<uint32_t, uint32_t>::iterator iter3 = da_map.find( some_func() );
std::cout << "sizeof<uint32_t>:" << sizeof(uint32_t) << " sizeof<uint64_t>:" << sizeof(uint64_t) << std::endl;
std::cout << "uint64_t(-1):" << uint64_t(-1) << " map:" << da_map.find(uint64_t(-1))->first << std::endl;
}
/**
evan@om2:~$ clang++ -c umap.cc -std=c++0x -pedantic -Wconversion
umap.cc:23:73: warning: implicit conversion from 'uint64_t' (aka 'unsigned long') to 'const key_type' (aka 'const unsigned int') changes value from 18446744073709551615 to 4294967295 [-Wconstant-conversion]
std::unordered_map<uint32_t, uint32_t>::iterator iter2 = da_map.find( uint64_t(-1) );
~~~~~~ ^~~~~~~~~~~~
umap.cc:24:73: warning: implicit conversion loses integer precision: 'uint64_t' (aka 'unsigned long') to 'const key_type' (aka 'const unsigned int') [-Wshorten-64-to-32]
std::unordered_map<uint32_t, uint32_t>::iterator iter3 = da_map.find( some_func() );
~~~~~~ ^~~~~~~~~~~
umap.cc:28:76: warning: implicit conversion from 'uint64_t' (aka 'unsigned long') to 'const key_type' (aka 'const unsigned int') changes value from 18446744073709551615 to 4294967295 [-Wconstant-conversion]
std::cout << "uint64_t(-1):" << uint64_t(-1) << " map:" << da_map.find(uint64_t(-1))->first << std::endl;
~~~~~~ ^~~~~~~~~~~~
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment