Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

Unable to create a templated function pointer to std::max

Levi_Morrison
Beginner
409 Views

The following code will not compile with icpc 15.0.2:

#include <algorithm>
#include <array>
#include <numeric>

template<typename T>
T const & (*maxer) (T const &, T const &) = std::max<T>;

int main(void) {
    std::array<float, 8> data;
    std::iota(data.begin(), data.end(), 0.0f);
    if (std::accumulate(data.begin(), data.end(), 0.0f, maxer<float>) == 7.0f) {
        return 0;
    } else {
        return 1;
    }   
}

Here is the output:

$ icpc -std=c++14 -Wall -Wextra max.cc 
max.cc(6): error: "maxer" is not a function or static data member
  T const & (*maxer) (T const &, T const &) = std::max<T>;
              ^

max.cc(11): error: identifier "maxer" is undefined
      if (std::accumulate(data.begin(), data.end(), 0.0f, maxer<float>) == 7.0f) {
                                                          ^

max.cc(11): error: type name is not allowed
      if (std::accumulate(data.begin(), data.end(), 0.0f, maxer<float>) == 7.0f) {
                                                                ^

max.cc(11): error: expected an expression
      if (std::accumulate(data.begin(), data.end(), 0.0f, maxer<float>) == 7.0f) {
                                                                      ^

compilation aborted for max.cc (code 2)

This code compiles successfully under clang++ and g++. As far as I can tell it is completely standards complaint. I suspect this is just a bug. I can work around it in the meantime by removing the template and hard-coding a type.

0 Kudos
6 Replies
KitturGanesh
Employee
409 Views

Hi Levi,
Yes, this is indeed a bug and thanks for catching this issue. I've filed with the developers and will update you as soon as the release with the fix is out. Appreciate your patience till then
_Kittur

0 Kudos
Levi_Morrison
Beginner
409 Views

Thank you for the prompt reply! In this particular case the work-around is trivial but inconvenient since you have to forward declare every type you intend to use. Looking forward to the fix.

0 Kudos
Judith_W_Intel
Employee
409 Views

 

You are trying to use the new c++14 feature variable templates which according to this table in not yet support in the Intel 15.0 (or 16.0) compiler:

https://software.intel.com/en-us/articles/c14-features-supported-by-intel-c-compiler

GNU just added support for this in their latest release. Glad to hear you have a workaround.

Judy

 

0 Kudos
Levi_Morrison
Beginner
409 Views
Thank you for the update. Are variable templates on the roadmap at all?
0 Kudos
KitturGanesh
Employee
409 Views

Hi Levi,
Thanks to Judy I just realized the support for variable template isn't there in the present version or in the upcoming 16.0 as well :(
I'll keep you updated as soon as the release with the support for this feature is out. 
_Kittur

0 Kudos
KitturGanesh
Employee
409 Views

Yes the 5.X (I tried 5.2) release (GNU) has this support (added recently) as Judy mentioned earlier.

_Kittur
 

0 Kudos
Reply