Bikineev
Bikineev

Reputation: 1777

C++ specialization with free functions types with const-qualififier

It's impossible to make free functions const-quilified, but what does the following specialization mean and when is it applied?

template<typename _Res, typename... _ArgTypes>
struct _Weak_result_type_impl<_Res(_ArgTypes...) const>
{ typedef _Res result_type; };

I can use this specialization the following way:

typedef _Weak_result_type_impl<int () const>::result_type type;

But what the function type is "int () const". When is it used?

Upvotes: 1

Views: 104

Answers (1)

quantdev
quantdev

Reputation: 23813

This const can be used (as pointed by 0x499602D2) to capture const member functions.

Consider the following example :

#include <iostream>

using namespace std;

class foo
{
    public:
     void bar1() { cout << "bar1\n"; }
     void bar2() const { cout << "bar2\n"; }
};

template <typename T, void (T::*mf)() const>
struct Test
{
   void call(T & obj) { (obj.*mf)(); }
};

int main()
{
  foo f;
  //Test<foo, &foo::bar1> t; // Doesn't compile
  //t.call(f);

  Test<foo, &foo::bar2> t2;
  t2.call(f);
  return 0;
}

The template Test is able to capture only const member functions (doesn't compile otherwise). You can easily imagine specializations based on constness of member functions, which might be what your code is doing (impossible to tell without more context)


Live Demo

Upvotes: 1

Related Questions