Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 568
Leo Avatar asked Sep 15 '25 03:09

Leo


1 Answers

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 ";});
like image 102
R Sahu Avatar answered Sep 16 '25 20:09

R Sahu