Reputation: 14869
I am using some function templates in order to store data generically and per type.
I defined my template<class T> store()
functions like below.
If I call the functions explicitly with the type uint64_t
store<uint64_t>( &p, my_value );
I receive a compile time error saying
call of overloaded 'store(char**, uint64_t&)' is ambiguous
candidates are:
void store(char**, T) [with T = long unsigned int]
void store(char**, T2) [with T1 = long unsigned int,
T2 = long unsigned int]
If instead I write it without type I have no error ( compiler is inferring the type ).
store( &p, my_value );
I noticed that the compiler is assuming a function having the second argument passed by reference, while my template declaration the second argument must be copied. Do you know why this happened and what to do to solve this ambiguity or being more clear at type declaration?
Best Regards
AFG
template< class T >
inline void store( char** buffer, T data ){
memcpy( *buffer, &data, sizeof( T ) );
buffer += sizeof(T);
}
template< class T1, class T2 >
inline void store( char** buffer, T2 data ){
memcpy( *buffer, &data, sizeof( T1 ) );
buffer += sizeof(T1);
}
Upvotes: 2
Views: 2919
Reputation: 26975
Do not use overload functions. Change the name of second procedure:
template< class T >
inline void store( char** buffer, T data ){
memcpy( *buffer, &data, sizeof( T ) );
buffer += sizeof(T);
}
template< class T1, class T2 >
inline void store_special( char** buffer, T2 data ){
memcpy( *buffer, &data, sizeof( T1 ) );
buffer += sizeof(T1);
}
Sample of using
store_special<uint64_t>( &p, my_value );
Upvotes: 1
Reputation: 12155
The compiler does not know which function implementation to take, since they both fit, and the details of what types are used for template classes are in the error message.
It looks like you can eliminate one method, this one:
template< class T >
inline void store( char** buffer, T data ){
memcpy( *buffer, &data, sizeof( T ) );
buffer += sizeof(T);
}
Upvotes: 0