3

I have a small templated struct that forwards a call to another templated struct. It should be simple. But I am facing a problem. The idea is that One struct uses the template parameter to forward a call depending on a compile time constant passed as parameter.

Here is the minimal code I manage to have to reproduce the problem:

struct OutFoo
{
template <bool Condition>
static constexpr void foo_ce() noexcept
{
}

static constexpr void foo() noexcept
{
}

};

template <typename OF>
struct InnerFoo
{
template <bool b>
static constexpr void inner_foo() noexcept
{
//    OF::foo_ce<b>();        // Does not compile
//    OF::foo_ce<true>();     // Does not compile
    OF::foo();
}
};

int main()
{
    InnerFoo<OutFoo>::inner_foo<true>();
}

My understanding is that both lines commented out should compile, given that OF (aka OutFoo should be taken and then foo_ce() should be called.

This actually works with MSVC but not with gcc. Who is right? What am I doing wrong?

https://godbolt.org/z/PoGv6r

gcc error:

source>: In static member function 'static constexpr void InnerFoo<OF>::inner_foo()':
<source>:22:19: error: expected primary-expression before ')' token
   22 |     OF::foo_ce<b>();
      |                   ^
<source>:23:22: error: expected primary-expression before ')' token
   23 |     OF::foo_ce<true>();
      |                      ^
<source>: In instantiation of 'static constexpr void InnerFoo<OF>::inner_foo() [with bool b = true; OF = OutFoo]':
<source>:30:39:   required from here
<source>:22:15: error: invalid operands of types '<unresolved overloaded function type>' and 'bool' to binary 'operator<'
   22 |     OF::foo_ce<b>();
      |         ~~~~~~^~
<source>:23:15: error: invalid operands of types '<unresolved overloaded function type>' and 'bool' to binary 'operator<'
   23 |     OF::foo_ce<true>();
      |         ~~~~~~^~~~~

clang error is more clear:

<source>:22:9: error: missing 'template' keyword prior to dependent template name 'foo_ce'
    OF::foo_ce<b>();
        ^     ~~~
<source>:23:9: error: missing 'template' keyword prior to dependent template name 'foo_ce'
    OF::foo_ce<true>();
        ^     ~~~~~~
2 errors generated.
Marek R
  • 32,568
  • 6
  • 55
  • 140
LeDYoM
  • 949
  • 1
  • 6
  • 21
  • Please paste the text of the error messages. – cigien Nov 26 '20 at 15:31
  • 1
    You need `OF::template foo_ce();`. – cigien Nov 26 '20 at 15:37
  • https://godbolt.org/z/4Webxz but it would be nice to have technical explanation why this is needed. – Marek R Nov 26 '20 at 15:37
  • Thanks, I see that the clang output from godbolt is actually more clear than the output from gcc that I was seeing. – LeDYoM Nov 26 '20 at 15:43
  • @cigien Can you explain why `template` keyword is required before template function `foo_ce` – Harry Nov 26 '20 at 15:43
  • I don't know what is your error code but I can tell you that Code Template is instantiated at a compiler time, for that reason the compiler needs to deduce the data type that the template will use. For that reason, It's better to code in a way that you give chance to the compiler, in an easy way, to deduce the type of the template. Consider that GCC has other rules to deduce the data type against MSVC, or it could be a simple bug of MSVC. – Jorge Omar Medra Nov 26 '20 at 15:44
  • Please read the target post. It covers this in quite a lot of detail. Also, as pointed out, you clearly haven't pasted the text of the error message into a search engine, and read any of the top results. I'm down-voting this for lack of research effort. – cigien Nov 26 '20 at 15:44
  • @super I was using gcc where the error message did not mention the missing template, but something unrelated: https://godbolt.org/z/v41jaG – LeDYoM Nov 26 '20 at 15:46
  • @super: this was my edit, I missed that I had clang not gcc enabled on godbolt and I've paste wrong version of error message. gcc error message is not very helpful. – Marek R Nov 26 '20 at 15:47
  • Oops, I didn't realize that someone else added the error message. As MarekR points out, gcc's diagnostic is not really helpful. Retracted my down-vote. – cigien Nov 26 '20 at 15:48
  • @cigien Actually I realise gcc and MSVC are not very good at exactly this error: missing template keyword when it is not trivial. I do not know why. With the CLang output I see the problem, but I did not even thought about a missing template. I would not post it as duplicate, since the root cause was not clear. But whatever you consider best. – LeDYoM Nov 26 '20 at 15:53
  • 1
    I don't really know why msvc and gcc have issues with diagnostic quality on this. But it's still a duplicate. The fact that you, very understandably, didn't recognize that is besides the point, I think. – cigien Nov 26 '20 at 15:55

0 Answers0