2

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:

C++11: How to alias a function?

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

#include <iostream>
namespace Bar
{
   void test()
   {
      std::cout << "Test\n";
   }

   template<typename T>
   void test2(T const& a)
   {
      std::cout << "Test: " << a << std::endl;
   }
}

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

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, 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?

Community
  • 1
  • 1
HonestMath
  • 325
  • 1
  • 2
  • 10
  • I dont see function noBarTest having template parameters...template . – Arunmu May 30 '16 at 16:48
  • 1
    Since this is your first question, I am assuming that you are new to stack overflow. So, Welcome! Use the comment section if you have any difficulty in understanding the solutions offered by others. If you find a working solution, you can accept the answer by clicking on the 'check' button below the counter. That will help you in getting more responses next time when you ask a question. – Arunmu May 30 '16 at 17:29
  • @Arunmu, Thank you for the welcome. I wasn't sure about when to use comments so now I know. You're right that I left off the template declaration in the code snippet. I had it in there but lost it in editing as I fumbled around trying to format things. – HonestMath May 30 '16 at 18:44

1 Answers1

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

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

int main() {
  return 0;
}

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

skypjack
  • 49,335
  • 19
  • 95
  • 187
Arunmu
  • 6,837
  • 1
  • 24
  • 46
  • Wow, that was a boneheaded mistake. Thanks for pointing it out! Is the const& a necessity or just good practice? – HonestMath May 30 '16 at 18:42
  • For native types such as char, int etc. It doesn't make a difference. Better pass by value. For user defined types which are kind of costly to copy, its better to pass by const reference if you don't want them to be changed at called site or by reference if you want to update your type's content at the called site. – Arunmu May 30 '16 at 18:46
  • @Matt You would find this post interesting: http://stackoverflow.com/questions/410593/pass-by-reference-value-in-c – Arunmu May 30 '16 at 18:50