VC++ 2012 variadic template problem

Started by
2 comments, last by BitMaster 11 years, 3 months ago

So I installed the November CTP compiler for VS2012 express edition to play around with some of the new C++11 features and across an interesting bug. The code:


template <typename T, typename Alloc> template <typename... TArgs>
	typename List<T,Alloc>::Node* List<T,Alloc>::AllocateNode (TArgs&&... args) {
	uint64_t m;
	Node* n = Allocate<Node>(1,m);
	try { new (n) Node(std::forward<TArgs>(args)...); }
	catch (...) { Deallocate(n); throw; }
	return n;
	}

Gives an "error C3546: '...' : there are no parameter packs available to expand" error at the catch(...) line. Apparently this is a known bug with the CTP as catch(...) statements can't be used with variadic templates. Fair enough that's why its a 'CTP'.

The question is, can anyone think of a workaround to this?
Advertisement
I can think of a really messy one. Create an object with a pointer member that calls Deallocate() on that pointer if it's not null, and construct it with n. Then after the new expression, set the pointer to null. (Or some moral equivalent like sticking a bool in it, etc.)

Makes sense, I think that would be ok for a temporary solution. Thank-you ;)

Shouldn't this work more intuitively?
template  template 
   typename List::Node* List::AllocateNode (TArgs&&... args) 
   {
      struct Deleter
      {
         void operator () (Node* n) { Deallocate(n); }
      };

      uint64_t m;
      std::unique_ptr<Node, Deleter> n(Allocate(1,m));
      new (n.get()) Node(std::forward(args)...);
      return n.release();
   }
Edit: initially forgot I need the deleter as part of the std::unique_ptr type...
Edit2: not what I planned initially, but this should be the same performance as doing it by hand, just with automatic RAII. I'm pretty sure the local type in the template should be legal in C++11 but I don't have a relevant standard at hand right now.

This topic is closed to new replies.

Advertisement