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

Leo

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?

R Sahu

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 ";});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

CUDA template error: no instance of function template matches the argument list

No instance of function template "std::make_pair" matches the argument list

no instance of function template matches the argument list i dont know why

OnComponentBeginOverlap.AddDynamic says no instance of the function template matches the argument list?

No instance of function template "max" matches the argument list argument types are (int, int)

No instance of function template matches the argument list, argument types are: (std::string, CommandLineArgumentTypes)

No instance of overloaded function matches the argument list

c++ template - More than one instance of overloaded function matches the argument list

How can I fix a "no instance of function template 'Defer' matches the argument list" error in C++?

C++ template class: No instance of constructor matches the argument list

Error No instance of overloaded function "getline" matches the argument list

no instance of constructor matches the argument list

no instance of "getline" matches the argument list

Trying to print an array argument error

no instance of constructor "AcademicStaff::AcademicStaff" matches the argument list

More than one instance of overloaded function matches the argument list and I can't find where the error happens

no instance of overloaded function "std::make_unique" matches the argument list, but works with unique_ptr constructor

No instance of overloaded function winrt::Windows::UI::Xaml::Controls::Primitives::SelectorItem::Content matches the argument list

C++ more than one instance of overloaded function matches the argument list when creating a header file

c++ struct in map as value - error "no instance of overloaded function matches the argument list"

C++ and SFML, no instance of the overloaded function "sf::RenderWindow.draw()" matches the argument list

c++ getline() error, no instance of overload function "getline" matches the argument list

How to fix "no instance of overloaded function "getline" matches the argument list" when using getline with an int

lambda function doesn't matches argument list

Using GetLine with ifstream - no instance of "getline" matches the argument list

Error: No instance of constructor "Faktura::Faktura" matches the argument list

no instance of constructor "std::thread::thread" matches argument list

C++ no instance of constructor matches the argument list.

How to fix an "no instance of constructor matches argument list" error in C++?