Calling a static function template in a dependent scope

Suppose I have a static function template<int I> void ft()

template inside a struct template template<bool B> S

and I want to call ft

from another function template by template<bool B> void g()

passing the bool template parameter from g

toS

template<bool B>
struct S {
  static void f() {
  }
  template<int I>
  static void ft() {
  }
};

template<bool B>
void g() {
  S<B>::f();
  S<B>::ft<12>();
}

int main() {
  g<true>();
  return 0;
}

      

Compiling in GCC 4.5.2 gives two errors regarding the line S<B>::ft<12>()

:

  • expected primary expression before ')' token
  • invalid operands of types '<unresolved overloaded function type>' and 'int' into binary 'operator <'

Comeau ( http://www.comeaucomputing.com/tryitout/ ), in C ++ 03 strict mode also complains about this line, specifying "expected expression" , with a caret just below the closing parenthesis. However, no compiler complains about the line S<B>::f()

, and Como can actually compile the entire minimal example in relaxed mode.

If I delete the template g

and instead instantiate S

the template parameter in the g

following way:

void g() {
  S<true>::ft<12>();
}

int main() {
  g();
  ...

      

GCC compiles it successfully, as does Como in C ++ 03 strict mode.

From the second GCC error above, there seems to be an ambiguity in interpretation S<B>::ft<12>

, as if it thinks I'm trying to check if S<B>::ft

less than 12. I am aware of using of typename

to disambiguate when referring to types within dependent scopes. Does anyone know how to resolve the ambiguity when the thing that appears in the dependent scope is a function and not a type?

+3


source to share


1 answer


You need to help the compiler a little by specifying that ft is a template, for example:



template<bool B>
struct S {
  static void f() {
  }
  template<int I>
  static void ft() {
  }
};

template<bool B>
void g() {
  S<B>::f();
  S<B>::template ft<12>();
}

int main() {
  g<true>();
  return 0;
}

      

+6


source







All Articles