Sign in to follow this  
Ryan_001

VC++ 2012 variadic template problem

Recommended Posts

Ryan_001    3475

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?
Edited by Ryan_001

Share this post


Link to post
Share on other sites
SiCrane    11839
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.)

Share this post


Link to post
Share on other sites
BitMaster    8651
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. Edited by BitMaster

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this