1

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?

Bikineev
  • 1,685
  • 15
  • 20
  • 1
    Your title is a bogus. Only member functions can have a `const-qualifier` that refers to their object. – 101010 Aug 14 '14 at 14:01
  • `_Res` could be a pointer to member function. Unfortunately your post is somewhat broad because you give no specific examples of usage either in your question or your code. Including those in your post would improve your question considerably. – Captain Obvlious Aug 14 '14 at 14:25
  • This is the meta-function defined in libstdc++ – Bikineev Aug 14 '14 at 14:45
  • So. There's nothing preventing you from declaring something like it yourself. Take the definition, paste it in your source file, and add `main` with examples of usage. – Captain Obvlious Aug 14 '14 at 15:02
  • I don't know usage, it was my question: "when is it applied?". Again, I found it in standard headers with NO usage. – Bikineev Aug 14 '14 at 15:45
  • I found an answer in related post http://stackoverflow.com/questions/17446220/c-function-types – Bikineev Aug 14 '14 at 18:33

1 Answers1

1

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

David G
  • 94,763
  • 41
  • 167
  • 253
quantdev
  • 23,517
  • 5
  • 55
  • 88
  • Are you sure that applies to this situation? There can be cv-qualified member functions and one can specialize a template based on their cv-qualification. – David G Aug 14 '14 at 14:18
  • @0x499602D2 Yes but you're not referring to top cv qualifiers of the arguments. Are you ? What is your reading then ? – quantdev Aug 14 '14 at 14:24
  • 1
    Isn't the `const` in `void (T::f)() const` top-level? That is what I am referring to. – David G Aug 14 '14 at 14:35
  • @0x499602D2 we discussed it in the and concluded that you were right (in fact the above example shows that this `const` is not ignored) – quantdev Aug 14 '14 at 18:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/59378/discussion-between-quantdev-and-0x499602d2). – quantdev Aug 14 '14 at 18:20
  • I think it's incorrect, because top-level cv-qualifiers are ignored for non-type parameters. We use const in specialization < > block. – Bikineev Aug 14 '14 at 18:22
  • @Bikineev The cv-qualifier of a function type is not top-level in that sense. See http://ideone.com/KnppoY and §8.3.5(6). – Oktalist Aug 14 '14 at 19:50