65,187
社区成员




template<typename K, typename V, typename _Predicate>
void do_something_to_all( std::map<K, V>& __can, const _Predicate& __pred )
{
for ( auto& item : __can ) __pred( item.second );
}
完整测试代码
#include <vector>
#include <map>
#include<stdio.h>
using namespace std;
struct test
{
int value;
test( int i = 0 ): value( i ) {}
void show_info( const char*, const char* )const
{
printf( "%d\n", value );
}
};
template<typename _Can, typename _Predicate>
void do_something_to_all( _Can& __can, const _Predicate& __pred )
{
for ( auto& item : __can ) __pred( item );
}
template<typename K, typename V, typename _Predicate>
void do_something_to_all( std::map<K, V>& __can, const _Predicate& __pred )
{
for ( auto& item : __can ) __pred( item.second );
}
int main( int argc, char const *argv[] )
{
auto func = []( const auto & item )
{
item.show_info( "", "" );
} ;
std::map<int, test> m1;
m1[1] = test( 111 );
m1[9] = test( 99 );
do_something_to_all( m1,func );
std::vector<test> v1;
v1.push_back( test( 222 ) );
v1.push_back( test( 333 ) );
do_something_to_all( v1, func );
return 0;
}
[/quote]
嗯,你这个确实能工作,作为free function的话,让编译器来选择,只是我需要需要的是类成员函数,因为我在do_something_to_all中需要lock一个mutex,于是我定义一个宏,并在类里面使用这个宏,这个宏就不能重载了,所以我还是写了两份代码,名字一样。[/quote]
类成员函数一样可以重在啊。
template<typename K, typename V, typename _Predicate>
void do_something_to_all( std::map<K, V>& __can, const _Predicate& __pred )
{
for ( auto& item : __can ) __pred( item.second );
}
完整测试代码
#include <vector>
#include <map>
#include<stdio.h>
using namespace std;
struct test
{
int value;
test( int i = 0 ): value( i ) {}
void show_info( const char*, const char* )const
{
printf( "%d\n", value );
}
};
template<typename _Can, typename _Predicate>
void do_something_to_all( _Can& __can, const _Predicate& __pred )
{
for ( auto& item : __can ) __pred( item );
}
template<typename K, typename V, typename _Predicate>
void do_something_to_all( std::map<K, V>& __can, const _Predicate& __pred )
{
for ( auto& item : __can ) __pred( item.second );
}
int main( int argc, char const *argv[] )
{
auto func = []( const auto & item )
{
item.show_info( "", "" );
} ;
std::map<int, test> m1;
m1[1] = test( 111 );
m1[9] = test( 99 );
do_something_to_all( m1,func );
std::vector<test> v1;
v1.push_back( test( 222 ) );
v1.push_back( test( 333 ) );
do_something_to_all( v1, func );
return 0;
}
[/quote]
嗯,你这个确实能工作,作为free function的话,让编译器来选择,只是我需要需要的是类成员函数,因为我在do_something_to_all中需要lock一个mutex,于是我定义一个宏,并在类里面使用这个宏,这个宏就不能重载了,所以我还是写了两份代码,名字一样。
template<typename K, typename V, typename _Predicate>
void do_something_to_all( std::map<K, V>& __can, const _Predicate& __pred )
{
for ( auto& item : __can ) __pred( item.second );
}
完整测试代码
#include <vector>
#include <map>
#include<stdio.h>
using namespace std;
struct test
{
int value;
test( int i = 0 ): value( i ) {}
void show_info( const char*, const char* )const
{
printf( "%d\n", value );
}
};
template<typename _Can, typename _Predicate>
void do_something_to_all( _Can& __can, const _Predicate& __pred )
{
for ( auto& item : __can ) __pred( item );
}
template<typename K, typename V, typename _Predicate>
void do_something_to_all( std::map<K, V>& __can, const _Predicate& __pred )
{
for ( auto& item : __can ) __pred( item.second );
}
int main( int argc, char const *argv[] )
{
auto func = []( const auto & item )
{
item.show_info( "", "" );
} ;
std::map<int, test> m1;
m1[1] = test( 111 );
m1[9] = test( 99 );
do_something_to_all( m1,func );
std::vector<test> v1;
v1.push_back( test( 222 ) );
v1.push_back( test( 333 ) );
do_something_to_all( v1, func );
return 0;
}
[/quote]
嗯,你这个确实能工作,作为free function的话,让编译器来选择,只是我需要需要的是类成员函数,因为我在do_something_to_all中需要lock一个mutex,于是我定义一个宏,并在类里面使用这个宏,这个宏就不能重载了,所以我还是写了两份代码,名字一样。[/quote]
类成员函数一样可以重在啊。[/quote]
是的,我就是不想修改这个类,现在我的办法跟你的类似,总之重写了另外一个do_somethine_to_all,对外接口保持了不变。
理想的情况是,一个类A,其里面有一个容器,容器的类型由使用者通过模板传递给类A,但类A想实现这个do_something_to_all函数(这个函数本身不应该再需要模板了,因为容器和自己都在同一个类里面),就不好做了。struct HASHCODE {
size_t operator() (const int &a) const {
cout << "called" << endl ;
return 0 ;
}
} ;
int main () {
std::unordered_map<int ,int> a ;
reinterpret_cast<std::unordered_map<int ,int ,HASHCODE> &> (a).find (0) ;
system ("pause") ;
return 0 ;
}
template<typename K, typename V, typename _Predicate>
void do_something_to_all( std::map<K, V>& __can, const _Predicate& __pred )
{
for ( auto& item : __can ) __pred( item.second );
}
完整测试代码
#include <vector>
#include <map>
#include<stdio.h>
using namespace std;
struct test
{
int value;
test( int i = 0 ): value( i ) {}
void show_info( const char*, const char* )const
{
printf( "%d\n", value );
}
};
template<typename _Can, typename _Predicate>
void do_something_to_all( _Can& __can, const _Predicate& __pred )
{
for ( auto& item : __can ) __pred( item );
}
template<typename K, typename V, typename _Predicate>
void do_something_to_all( std::map<K, V>& __can, const _Predicate& __pred )
{
for ( auto& item : __can ) __pred( item.second );
}
int main( int argc, char const *argv[] )
{
auto func = []( const auto & item )
{
item.show_info( "", "" );
} ;
std::map<int, test> m1;
m1[1] = test( 111 );
m1[9] = test( 99 );
do_something_to_all( m1,func );
std::vector<test> v1;
v1.push_back( test( 222 ) );
v1.push_back( test( 333 ) );
do_something_to_all( v1, func );
return 0;
}