3

The following code does not compile with clang 5.0.0 (compilation flags are -std=c++14 -Wall -Wextra -Werror -pedantic-errors -O0):

struct foo
{
};

int main()
{
    foo f;

    f.~decltype(f)();          // OK
    f.template ~decltype(f)(); // OK

    int i{};

    i.~decltype(i)();          // OK
    i.template ~decltype(i)(); // error: expected unqualified-id
}

Is it a way to force the compilation of a pseudo-destructor call with the template keyword?

Constructor
  • 7,273
  • 2
  • 24
  • 66
  • 2
    But why? It's not a template. I'm surprised `f.template ~decltype(f)();` compiles. What's the ultimate problem you are trying to solve, for which you believe adding `template` keyword would be helpful? – Igor Tandetnik Oct 21 '17 at 19:00
  • 1
    I'm not sure the first one should be accepted either. It's not a template in either case. – StoryTeller - Unslander Monica Oct 21 '17 at 19:00
  • @Rakete1111 - Strictly speaking, the `decltype` one for `foo` is valid. Good to know GCC is buggy too :P – StoryTeller - Unslander Monica Oct 21 '17 at 19:02
  • @IgorTandetnik `template` and `typename` keywords can be used outside templates since C++14. – Constructor Oct 21 '17 at 19:02
  • @StoryTeller *g++* has a bug here: it can't compile a pseudo-destructor call with the `decltype` keyword. – Constructor Oct 21 '17 at 19:03
  • @StoryTeller Whaaat. So weird. Thanks :) – Rakete1111 Oct 21 '17 at 19:03
  • 3
    @Constructor, The latest draft has *A name prefixed by the keyword template shall be a template-id or the name shall refer to a class template or an alias template. [ Note: The keyword template may not be applied to non-template members of class templates. — end note ]* – chris Oct 21 '17 at 19:04
  • @chris 'Latest draft' means C++14+ or C++17+? – Constructor Oct 21 '17 at 19:06
  • @Constructor - [Here it is in n4140](https://timsong-cpp.github.io/cppwp/n4140/temp#names-5). Which is pretty much the same as the official C++14. The change you have in mind is about allowing it to refer to **templates** outside of a template context. – StoryTeller - Unslander Monica Oct 21 '17 at 19:08
  • 1
    BTW, @chris, that's an answer. You should post it as one. – StoryTeller - Unslander Monica Oct 21 '17 at 19:10
  • @chris OK. So even `f.template ~decltype(f)();` is illegal, isn't it? – Constructor Oct 21 '17 at 19:10
  • C++17, specifically, [this PDF](https://timsong-cpp.github.io/cppwp/draft.pdf), which is unofficial, but convenient. It's in [temp.names]/5, and hasn't changed from N4140 (C++14). Anyway, I don't know why any of these would work, but I'm not that knowledgeable in this area to begin with. – chris Oct 21 '17 at 19:11

1 Answers1

4

As far as I can tell, [temp.names]/5 prohibits both of these .template … lookups:

A name prefixed by the keyword template shall be a template-id or the name shall refer to a class template or an alias template. [ Note: The keyword template may not be applied to non-template members of class templates. — end note ]

None of these destructor names are template-ids, nor do they refer to class templates or alias templates. However, it's possible that I'm missing something.

chris
  • 60,560
  • 13
  • 143
  • 205