Skip to content
Why is a raven like a writing desk?

Thoughts both confusing and enlightening.

Why is a raven like a writing desk?

Thoughts both confusing and enlightening.

C++ Guru Question – followup

elbeno, 12 August, 201430 June, 2015

(following on from C++ Guru Question)

There are a few reasons why the code before didn’t work: mainly

a) C++ template argument deduction works one-way with a list of candidates, it’s not H-M type inference.
b) A C++ lambda is a thing with some internal type, not a std::function (although it can be assigned to one, that doesn’t matter for template type deduction).

Anyway, a reasonable solution to this problem is to simplify the template argument matching to the function type, and use a simple function_traits template to take apart the function argument and return types.

template 
struct function_traits
  : public function_traits
{};

template 
struct function_traits
{
  typedef R returnType;
  typedef A argType;
};

template 
struct function_traits
  : public function_traits
{};

template 
struct Foo
{
  T m_t;
};

template 
typename function_traits::returnType operator/=(
    Foo::argType> foo, const F& fn)
{
  return fn(foo.m_t);
}

void mystery()
{
  auto foo = Foo{1};

  // this works...
  function(int)> f1 = [] (int i) { return Foo{i}; };
  auto bar1 = foo /= f1;

  // this does too!
  auto f2 = [] (int i) { return Foo{i}; };
  auto bar2 = foo /= f2;
}
C++ Programming

Post navigation

Previous post
Next post

Related Posts

fighting the code

25 March, 200729 July, 2007

Why doesn’t automake/autoconf properly recognise the way to use Flex/Bison with C++ scanner/parser generation? I use a .ll file and it thinks the output is going to be a .c instead of a .cc. Likewise I use a .yy file and it screws up the includes in my bison output…

Read More

Functional Fills with Vecto

25 March, 200825 March, 2008

I’ve been wanting to do exclusive-or functional fill in vecto for a while, so tonight I delved in. I added a pixel function to the graphics state and kept the default as the normal alpha-blending it was already doing. The pixel function signature is a bit clumsy, but it was…

Read More

How to print anything in C++ (part 4)

2 February, 201530 June, 2015

Part 1 Part 2 Part 3 Part 4 Part 5 Postscript Callable things. There are several types: functions member functions std::functions bind expressions lambdas objects that support operator() (function objects) So, going back to my tag code, so far (with everything I’ve added) and including callable things, it will look…

Read More

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

©2026 Why is a raven like a writing desk? | WordPress Theme by SuperbThemes