Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deducing function template parameters from template argument types

Tags:

c++

templates

I'm currently trying to write a function which takes an STL array of any type as one of its parameters. The obvious way to write it is:

template<typename T, int count>
void setArrayBufferData(GLenum usage, const std::array<T, count>& data) {
  setArrayBufferData(usage, data.data(), sizeof(T) * count);
}

And here's the other overload which it calls just for reference

void setArrayBufferData(GLenum usage, void* data, int size) {
  glBufferData(GL_ARRAY_BUFFER, size, data, usage);
}

The function definition compiles fine. However, when I try to call it

std::array<int, 4> data;
setArrayBufferData(GL_STATIC_DRAW, data);

I get a "No matching function for call to 'setArrayBufferData'" error message. I know if I specified the template parameters in the call it would work, but I want the call to deduce them. I've tried researching template template parameters, a more generic declaration followed by an std::array specialization, and every other syntactical variation I could think of, yet I can't seem to figure out a way to get what I'm looking for. Is it even possible, and if so, what needs to be done?

like image 737
jbatez Avatar asked Jul 16 '12 13:07

jbatez


People also ask

Can we pass Nontype parameters to templates?

Template classes and functions can make use of another kind of template parameter known as a non-type parameter. A template non-type parameter is a template parameter where the type of the parameter is predefined and is substituted for a constexpr value passed in as an argument.

What are template arguments enlist types of template arguments?

A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)

What is template type parameter?

A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.

Which template can have multiple parameters?

C++ Templates: Templates with Multiple Parameters | C++ Tutorials for Beginners #65.


1 Answers

template<typename T, int count>
void setArrayBufferData(GLenum usage, const std::array<T, count>& data)

is incorrect, since std::array is template<typename T, size_t N> struct array. The second parameter must be of type size_t, not int.

Also, data.data() returns const T* since data is const reference to std::array, so, try to setArrayBufferData(GLenum usage, const void* data, int size) or call it with setArrayBufferData(usage, const_cast<T*>(data.data()), sizeof(T) * count);

#include <array>

void function(const void* ptr, size_t bytes)
{
}

template<typename T, size_t count>
void function(const std::array<T, count>& array)
{
   function(array.data(), sizeof(T) * count);
}

int main()
{
   std::array<int, 4> array;
   function(array);
}

this example works fine. http://liveworkspace.org/code/2a5af492e1f4229afdd0224171854d1c

like image 198
ForEveR Avatar answered Sep 28 '22 06:09

ForEveR