Friday 3 November 2017

c++11 - alias for multi parameter function template

itemprop="text">

I am trying to create a template for a
multi-parameter function, and then an alias for a particular instantiation. From this
really good post:



href="https://stackoverflow.com/questions/9864125/c11-how-to-alias-a-function">C++11:
How to alias a function?




I
found example code that works for a single function parameter and single template
parameter:



#include

namespace Bar
{
void test()

{
std::cout << "Test\n";
}



template
void test2(T const& a)
{

std::cout << "Test: " << a << std::endl;

}
}

void (&alias)() = Bar::test;
void
(&a2)(int const&) = Bar::test2;


int
main()
{
Bar::test();
alias();

a2(3);
}


When
I try to expand to two function parameters as
such:




void noBarTest(T
const& a, T const& b)
{
std::cout << "noBarTest: "
<< a << std::endl;
}

void(&hh)(int
const&, int const&) = noBarTest int>;


I get these
errors in Visual
Studio:






error C2440: 'initializing' : cannot convert from 'void (__cdecl

*)(const T &,const T &)' to 'void (__cdecl &)(const int &,const int
&)'



IntelliSense: a reference of type
"void (&)(const int &, const int &)"
(not const-qualified) cannot
be initialized with a value of type

""





I
thought I followed the pattern exactly in expanding to 2 arguments.
What's
the proper syntax for this?


itemprop="text">
class="normal">Answer




template             T>
void noBarTest(T const& a, T const&
b)
{
}

void(&hh)(int const&, int
const&) = noBarTest; // Only once

int main()
{

return
0;
}


The
type parameter int needs to be specified only once in
noBarTest.



No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...