Using const char** with Template Specialization - c++

I am trying to write a template specialization for a function that returns the maximum value of an array of numeric values (general version) or the longest c-string of an array of c-strings (specialization). If I do not use const-ness, my function prototypes look like this
template <typename T>
T maxn(T* my_Tptr, unsigned int n);
template <>
char* maxn <char*> (char** my_cstrings, unsigned int n);
and my code compiles.
However, if I try to use const-ness, my function prototypes look like this,
template <typename T>
T maxn(const T* my_Tptr, unsigned int n);
template <>
char* maxn <char*> (const char** my_cstrings, unsigned int n);
my code does not compile, and the compiler (gcc) prints this error:
error: template-id 'maxn' for 'char* maxn(const char**, unsigned int)' does not match any template declaration.
Where am I going wrong?

The problem is in the constness. If you look closely const T* my_Tptr means my_Tptr is a pointer to const T. But const char** my_Tptr means Tptr is a pointer to pointer to const char. So the type moves from pointer to const T to pointer to pointer to const T. If you make it char* const* my_Tptr* then it will work, since then the type will be pointer to const char pointer. The specialization is pointer to const T*->pointer to const char*

Not sure what is the whole logic behind it but if you change your template definition to say that you are expecting pointers that will help:
template <typename T>
T* maxn(const T** my_Tptr, unsigned int n);
template <>
char* maxn(const char** my_cstrings, unsigned int n);

You might provide several overloads for the char-case to resolve the issue:
#include <iostream>
#include <stdexcept>
template <typename T>
T maxn(const T* const data, unsigned int n) {
throw std::logic_error("Failure");
}
const char* maxn(const char * const * data, unsigned int n) {
return "Success";
}
inline const char* maxn(const char** data, unsigned int n) {
return maxn(static_cast<const char * const *>(data), n);
}
inline const char* maxn(char* const * data, unsigned int n) {
return maxn(static_cast<const char * const *>(data), n);
}
inline const char* maxn(char** data, unsigned int n) {
return maxn(static_cast<const char * const *>(data), n);
}
int main() {
const char* a[] = { "A", "B", "C" };
std::cout << maxn((const char * const *)a, 3) << '\n';
std::cout << maxn((const char **)a, 3) << '\n';
std::cout << maxn((char * const *)a, 3) << '\n';
std::cout << maxn((char**)a, 3) << '\n';
}

This compiles fine:
template <>
char* maxn(char* const* my_cstrings, unsigned int n);
It accepts a pointer to a const char pointer like specified in the base template.

Related

no instance of function template matches the argument list (trying to print array)

When trying to compile this
template<typename type,const char* format>__device__ void d_prettyPrintVector(type* v, const unsigned int size)
{
printf("\n ");
for(int i=0; i<size; i++)
printf(format, v[i]);
}
template<> void d_prettyPrintVector<float, "%4.1f ">(float*, const unsigned int);
template<> void d_prettyPrintVector<int, "%2d " >(int*, const unsigned int);
template<> void d_prettyPrintVector<char, "%2d " >(char*, const unsigned int);
template<> void d_prettyPrintVector<bool, "%d" >(bool*, const unsigned int);
and use it like this
d_prettyPrintVector(dc, blockDim.x);
I am getting
kernels.cu(104): error: no instance of function template "d_prettyPrintVector" matches the argument list
argument types are: (int *, const unsigned int)
what is wrong?
I think you are not clear on how to use the type, float, int, etc., to grab an appropriate format string.
You can re-design your function to look something like:
template <typename type>
void d_prettyPrintVector(type* v, const unsigned int size)
{
printf("\n");
for(int i=0; i<size; i++)
printf(getFormatString<type>(), v[i]);
// ^^^ Get an format string appropriate for the type.
}
That would be perfectly valid code if you had a function template:
template <typename type> char const* getFormatString();
that had specializations for the types that you are interested in. In other words, the following should work:
template <typename type> char const* getFormatString();
template <typename type>
void d_prettyPrintVector(type* v, const unsigned int size)
{
printf("\n");
for(int i=0; i<size; i++)
printf(getFormatString<type>(), v[i]);
// ^^^ Get an format string appropriate for the type.
}
template <> char const* getFormatString<float>() { return "%4.1f "; }
template <> char const* getFormatString<int>() { return "%2d "; }
template <> char const* getFormatString<char>() { return "%2d "; }
template <> char const* getFormatString<bool>() { return "%1d "; }
Now, you can use:
int a[] = {1, 2};
d_prettyPrintVector(a, 2);
float b[] = {1.1f, 2.2f};
d_prettyPrintVector(b, 2);
without any problem.
Extra
You can extend that idea to provide a lambda function as an argument to d_prettyPrintVector. The lambda function can return a custom format string that is more appropriate for a single use case.
Overload d_prettyPrintVector. Provide one that can take a lamba function as an argument.
template <typename type, typename Format>
void d_prettyPrintVector(type* v, const unsigned int size, Format format)
{
printf("\n");
for(int i=0; i<size; i++)
printf(format(), v[i]);
}
You can even implement the initial function using the new function so you don't have to repeat the details of how the items are printed.
template <typename type> char const* getFormatString();
template <typename type>
void d_prettyPrintVector(type* v, const unsigned int size)
{
d_prettyPrintVector(v, size, [](){return getFormatString<type>();});
}
Now, in addition to the previous calls to print a and b, you can now use:
// Define the format on the fly, using a lambda function.
d_prettyPrintVector(a, 2, []() -> char const* {return "%4d ";});
// Define the format on the fly, using a lambda function.
d_prettyPrintVector(b, 2, []() -> char const* {return "%.6f ";});

array decay to pointer and overload resolution

I want to be able to differentiate array from pointers in overload resolution :
class string {
public:
string(const char* c_str);
template<int N>
string(const char (&str) [N]);
};
int main() {
const char* c_str = "foo";
string foo(c_str); // ok will call string(const char*)
string bar("bar"); // call string(const char*) instead of the array version
}
The best I have found so far is to use a reference to the pointer instead of a pointer :
class string {
public:
string(const char*& c_str);
template<int N>
string(const char (&str) [N]);
};
int main() {
const char* c_str = "foo";
string foo(c_str); // ok will call string(const char*)
string bar("bar"); // ok, will call the array version
}
it's not exactly the same thing and I want to know if a better way exist
You need to make the first overload a poorer choice when both are viable. Currently they are a tie on conversion ranking (both are "Exact Match"), and then the tie is broken because non-templates are preferred.
This ought to make the conversion ranking worse:
struct stg
{
struct cvt { const char* p; cvt(const char* p_p) : p(p_p) {} };
// matches const char*, but disfavored in overload ranking
stg(cvt c_str); // use c_str.p inside :( Or add an implicit conversion
template<int N>
stg(const char (&str) [N]);
};
You can use SFINAE. This might not be the best way, but it should work ok:
//thanks to dyp for further reduction
template<typename T, typename = typename std::enable_if<std::is_same<T, char>::value>::type>
string(const T * const &) {std::cout << "const char *\n";}
template<std::size_t N> //credit to jrok for noticing the unnecessary SFINAE
string(const char(&)[N]) {std::cout << "const char(&)[" << N << "]\n";}
Here's a live example.
A more generic version of this problem could be detected as follows.
template <class T>
void func(T,
typename std::enable_if<std::is_pointer<T>::value, void>::type * = 0)
{
// catch ptr
}
template <class T, int N>
void func(T (&)[N])
{
//catch array
}
int main(void)
{
int arr[5];
char *b = 0;
func(arr); // catch array
func(b); // catch ptr
}

why this code call general template function in GCC?

#include<iostream>
using namespace std;
// define the general compare template
template <class T>
int compare(const T& t1, const T& t2) {
cout<< "Common_T"<<endl;
return 0;
}
template<>
int compare<const char*>( const char * const& s1,
const char * const& s2)
{
cout<< "Special_T"<<endl;
return 0;
}
typedef const char char6[6];
template<>
int compare<char6>(const char6& s1,const char6& s2)
{
cout << "Special_Char6_T" << endl;
return 0;
}
int main() {
int i = compare("hello" , "world");
}
the result is:
Common_T
My question is: why don't output "Special_Char6_T"???
This is the correct template specialization that matches your c strings.
typedef char char6[6];
template<> int compare<char6>(char6 const &s1,char6 const &s2)
{
cout << "Special_Char6_T" << endl;
return 0;
}
Intuitively, because the dimension of arrays is not part of their type. That dimension matters only for variables and fields. So both
char hello[6]="hello";
and
char longstring[] = "a very very long string should be here";
have both the same type char[]
and
typedef char t1[6];
and
typedef char t2[];
are aliases for the "same" type.
(You could look at the mangled function names to have a clue)

const char array (c style string) template specialization

I want to be able to specialize based on a constant c style string. The problem is that when I call my templatized function the type is const char[N] where 'N' is the size of the string +1 (null character). How can I specialize for all c-style strings?
The following code displays the problem. You can see the specialization for const char [15] gets matched for "const char [15]" but for "const char[5]" it goes to Generic.
Is there any way to do this?
template <typename T>
struct Test {
static const char* type() { return "Generic"; }
};
template <>
struct Test<const char*> {
static const char* type() { return "const char*"; }
};
template <>
struct Test<const char[]> {
static const char* type() { return "const char[]"; }
};
template <>
struct Test<const char[15]> {
static const char* type() { return "const char[15]"; }
};
template <>
struct Test<char*> {
static const char* type() { return "char*"; }
};
template <>
struct Test<char[]> {
static const char* type() { return "char[]"; }
};
template <typename T>
void PrintType(const T& expected) {
std::cerr << expected << " type " << Test<T>::type() << std::endl;
}
int main(int argc, char* argv[]) {
const char* tmp = "const char*";
PrintType(tmp);
PrintType("const char[]");
PrintType("const char[15]");
PrintType("const char[5]");
}
output when run in Windows 7 - VS 2008
const char* type const char*
const char[] type Generic
const char[15] type const char[15]
const char[5] type Generic
A specialization for any array of chars is:
template< std::size_t N >
struct Test< const char[N] > { ... };
However you can no longer return a char* from type(), unless you write more metafunctions to turn a non-type template parameter into its textual representation.
Try this:
#include <cstdio>
#include <string>
template<class T>
void f2(const T& s) // Handle all kinds of string objects
{ std::printf("string object: %s\n", s.c_str()); }
void f2(const char* s) // Handle const char*
{ std::printf("const char*: %s\n", s); }
// ----------------------------------------------------------------------------
template<size_t N>
void f(const char(&s)[N]) // Handle const char array
{ std::printf("const char[%zu]: %s\n", N, s); }
template<size_t N>
void f(char(&s)[N]) // Handle char array
{ std::printf("char[%zu]: %s\n", N, s); }
template<class T>
inline void f(T&& s) // Handle other cases
{ f2(std::forward<T>(s)); }
int main() {
std::string stringObj = "some kind of string object ...";
char charArr[] = "char array";
const char constCharArr[] = "const char array";
const char* constCharPtr = "const char pointer";
f(stringObj);
f(charArr);
f(constCharArr);
f(constCharPtr);
//f("const char array");
}
Output:
string object: some kind of string object ...
char[11]: char array
const char[17]: const char array
const char*: const char pointer
Explanation
f() has two kinds of overloads: One for char arrays and one for "everything else".
f2() handles the "everything else" case.

C++ function pointers inside templates

I have this problem:
template<typename T> class Bubu
{
...
int (*comparer)(const T t1, const T t2);
...
public:
Bubu(int (*_comparer)(const T t1, const T t2))
{
comparer = _comparer;
}
};
And in another file:
Bubu<char*> asd(strcmp);
Error:
error C2664: 'Bubu<T>::Bubu(int (__cdecl *)(const T,const T))' :
cannot convert parameter 1 from 'int (__cdecl *)(const char *,
const char *)' to 'int (__cdecl *)(const T,const T)'
I don't understand why. Shouldn't the compiler see a "char*" instead of "T" there?
EDIT: the Ideone.com-ready code:
int asdf(const char* a, const char* b)
{ return 0; }
template class Bubu
{
int (*comparer)(const T t1, const T t2);
public:
Bubu(int (*_comparer)(const T t1, const T t2))
{
comparer = _comparer;
}
};
int main(int argc, char* argv[])
{
Bubu asd(asdf);
}
When T is char*, const T is char* const which isn't the same thing as const char *. You need:
Bubu<const char*> asd(strcmp);
Top level const is ignored for function signatures so
int (*)( const char* const, const char* const );
is the same type as
int (*)( const char*, const char* );
so you're OK on the extra top level const although it doesn't gain you anything over the simpler int (*comparer)(T t1, T t2);.
If T is specialized as char*, const T means char* const (i.e. immutable pointer to a mutable char), rather than const char* == char const* (i.e. mutable pointer to an immutable char).
Bubu<const char*> asd(strcmp)
will compile.
Not sure if this is your problem, but your * in your function pointer declaration is in the wrong place (at least compared to what I've ever seen). Instead of:
int (comparer*)(const T t1, const T t2);
It should be:
int (*comparer)(const T t1, const T t2);
I think this does what you want:
#include <cstring>
#include <iostream>
template<class T>
class Bubu {
public:
typedef int (*Comparator)(T, T);
Bubu(Comparator inComparator) : mComparator(inComparator) { }
int compare(T a, T b)
{
return mComparator(a, b);
}
private:
Comparator mComparator;
};
int main()
{
Bubu<const char*> obj(strcmp);
std::cout << obj.compare("one", "two") << std::endl;
return 0;
}