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

Issue with static_cast and decltype

H_K_
Beginner
388 Views

The following code fails to compile with Intel C++ 17.0 but compiles fine with MSVC & gcc. If I use a typedef instead of decltype, then it compiles OK.

#include <iostream>

template<class Sig, Sig Func> struct MyFunc;
template<class T, class... Args, void (T::*Func)(Args...) const>
struct MyFunc<void (T::*)(Args...) const, Func>
{
  static void execute(const T *obj, Args... args)
  {
    (obj->*Func)(args...);
  }
};
template<class T, class... Args, void (T::*Func)(Args...)>
struct MyFunc<void (T::*)(Args...), Func>
{
  static void execute(T *obj, Args... args)
  {
    (obj->*Func)(args...);
  }
};

#define MYFUNC(...) MyFunc<decltype(__VA_ARGS__), __VA_ARGS__>

struct A
{
  void func() const { std::cout << "in A::func() const" << std::endl; }
  void func() { std::cout << "in A::func()" << std::endl; }
};

int main()
{
  A a;
  MYFUNC(static_cast<void(A::*)() const>(&A::func))::execute(&a);
  MYFUNC(static_cast<void(A::*)()>(&A::func))::execute(&a);
};
0 Kudos
2 Replies
Viet_H_Intel
Moderator
388 Views

What GCC version and the command line options did you use?

0 Kudos
H_K_
Beginner
388 Views

This is compiling with the following compilers:

  • gcc 6.3.0 (with no special compilation flags, or with -std=c++11)
  • Visual Studio 17.0 (with no special compilation flags)

It is failing with Intel 19.0 Update 3 (Windows) with /Qstd=c++14 but seems to work with /Qstd=c++17.

 

0 Kudos
Reply