1

I know the question is rather theoretical but I think if placeholders would be defined as the template like e.g.:

namespace std { 
   namespace placeholders {
      template <size_t> struct placeholder { constexpr placeholder() {}; }; 
      template <size_t N> constexpr placeholder<N> _{};
   }
}

with usage:

std::bind(foo, std::placeholders::_<1>, std::placeholders::_<2>); 

Or for c++11:

namespace std { 
   namespace placeholders {
      template <size_t> struct _ { }; 
   }
}

with usage:

std::bind(foo, std::placeholders::_<1>{}, std::placeholders::_<2>{});

the code wouldn't loose anything of its clearness and we would be able to do some fancy metaprogramming using it. So... why aren't placeholders for std::bind implemented using non-type template parameters?

W.F.
  • 13,888
  • 2
  • 34
  • 81
  • 7
    That syntax doesn't look legal. `_<1>` is still a type, and you cannot pass a type as a value: you'd need `_<1>{}` at the least, no? Or `template struct placeholder {constexpr placeholder() {};}; template constexpr placeholder _{};`? And when you get to the 2nd, well then you'd need variable templates, which did not exist in C++11. So, what do you want to know? – Yakk - Adam Nevraumont Aug 09 '16 at 16:09

1 Answers1

7

Variable templates did not exist in C++11, which is where std::bind was added to the language.

The _1 names where short, and taken from boost where std::bind was developed.

You can write your own similar placeholders easily.

namespace my_placeholders {
  template <int> struct placeholder { constexpr placeholder() {}; }; 
  template <int N> constexpr placeholder<N> _{};
}
namespace std {
  template<int N>
  struct is_placeholder< ::my_placeholders::placeholder<N> >:
    std::integral_constant<int, N>
  {};
}

and now my_placeholders::_<1> is a valid std::bind placeholder equivalent to _1 in every important way.

Given the ability to do this, and frankly how annoying it is to work with std::bind in comparison to lambda, I could see nobody bothering to actually add such a feature to the standard post C++14.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524