Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6187453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:03:39+00:00 2026-05-24T02:03:39+00:00

I want to explicitly destroy a vector in a templated context. The following works

  • 0

I want to explicitly destroy a vector in a templated context. The following works for me (GNU C++ 4.3, 4.4 and Clang++ 1.1):

template <typename T>
void destroy_vector_owner(VectorOwner<T> *obj)
{
    obj->v.~vector();
    // further cleanup by Python API functions omitted
}

while it fails on Mac OS X v10.5’s g++ (i686-apple-darwin10-gcc-4.2.1) with

expected class-name before ‘(’ token

If I change it to

obj->v.~vector<T>();

the code fails to compile with G++, but Clang can still handle it. Which is the correct idiom? Are any of these compilers known to be broken in this regard?

Update: the definition of VectorOwner is

template <typename T>
struct VectorOwner {
  PyObject_HEAD
  std::vector<T> v;
};

This is a Python object that has to keep an std::vector alive. I admit that the construct is slightly dangerous, but I need the compact storage, amortized O(1) push_back and the ability to steal another vector’s contents with the swap member.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:03:40+00:00Added an answer on May 24, 2026 at 2:03 am

    My first answer was wrong actually, litb pointed me into the right direction. The right answer is
    that both syntaxes are correct:


    Destructor call syntax.

    The syntax for an explicit destructor call is described in 12.4 Destructors:

    12  In an explicit destructor call, the destructor name appears
        as a ˜ followed by a type-name that names the destructor’s 
        class type. The invocation of a destructor is subject to the
        usual rules for member functions (9.3) [...]
    

    type-name can be found in 7.1.5.2 Simple type specifiers:

    type-name:
        class-name
        enum-name
        typedef-name
    

    class-name is described in 9. Classes:

    class-name:
        identifier
        template-id
        
    

    So a destructor call is, simplified, one of the following

    foo.~typedef-name ()
    foo.~identifier   ()
    foo.~template-id  ()
    

    We neither have a typedef-name here, nor a simple identifier, so only foo.~template-id() is left
    for us.


    Compiler’s assumption on destructor call with template-arguments.

    We also find in 14. Templates

    3 After name lookup (3.4) finds that a name is a template-name,
      if this name is followed by a <, the < is always taken as the
      beginning of a template-argument-list and never as a name
      followed by the less-than operator.
      
    

    So the compiler must assume in your example that the < is the beginning
    of a template-argument-list.

    Also, if your destructor would be a template (…), then

    4   When the name of a member template specialization appears 
        after . or -> in a postfix-expression, or after nested-name-specifier
        in a qualified-id, and the postfix-expression or qualified-id explicitly
        depends on a template-parameter (14.6.2), the member template name must
        be prefixed by the keyword template. Otherwise the name is assumed to 
        name a non-template.
    

    So because you did not prefix your destructor call f.~foo<int> with template, i.e.
    like f.template ~foo<int>, the compiler must assume that your destructor
    is NOT a template.

    Backtrack.

    Further,

    6   A template-id that names a class template specialization
        is a class-name (clause 9).
    

    So ~foo<int> names your template specialization foo<int> and therefore is a class-name,
    a class-name is by the grammar rules a type-name, and a ~ followed by a typename is
    a destructor call. Therefore

    foo<int> f;
    f.~foo<int>(); // valid
    

    Destructor call without template-arguments.

    But also

    f.~foo(); // valid
    

    Because 3.4.5 Class member access:

    3 If the unqualified-id is ˜type-name, and the type of the object expression
      is of a class type C (or of pointer to a class type C), the type-name is
      looked up in the context of the entire postfix-expression and in the scope of
      class C. [...]
      
    

    thus in f.~foo();, foo is looked up within f., and within the scope of foo<int>, it is valid
    to refer to it just with with foo.


    The standard is actually explicit on this topic, d’oh.

    And finally, 14.3 contains the one-and-for-all-permission:

    5   An explicit destructor call (12.4) for an object that 
        has a type that is a class template specialization may
        explicitly specify the template-arguments. [Example:
    
          template<class T> struct A {
              ˜A();
          };
          void f(A<int>* p, A<int>* q) {
              p->A<int>::˜A();      // OK: destructor call
              q->A<int>::˜A<int>(); // OK: destructor call
          }
    
        —end example]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why one would want to explicitly clear the a vector member variable (of on
I have many cookie-cutter spring beans and don't want to explicitly define each one
I have an app that I want to be launchable either explicitly when tapped
I am running Tomcat on a small VPS (256MB/512MB) and I want to explicitly
I want to explicitly initialize some classes during the initialization of my application using
I am using single table inheritance in my rails application, and want to explicitly
I want to explicitly map fields in my classes to XML, so I'm using
I want to do color animations without explicitly specifying absolute colors. To give you
I want to duplicate a row, not the keys of course, without explicity using
I want to add explicit lock on row which is currently being updated and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.