Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


VC++ 2012 variadic template problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Ryan_001   Prime Members   -  Reputation: 1433

Like
0Likes
Like

Posted 09 January 2013 - 11:37 AM

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, 09 January 2013 - 12:30 PM.


Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9626

Like
3Likes
Like

Posted 09 January 2013 - 06:15 PM

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.)

#3 Ryan_001   Prime Members   -  Reputation: 1433

Like
0Likes
Like

Posted 09 January 2013 - 07:19 PM

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



#4 BitMaster   Crossbones+   -  Reputation: 4233

Like
1Likes
Like

Posted 10 January 2013 - 03:09 AM

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, 10 January 2013 - 03:17 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS