Quantcast
Channel: Intel® Software - Intel® C++ Compiler
Viewing all articles
Browse latest Browse all 2797

Variadic constructor with enable_if not found? (C++11)

$
0
0

I'm having a bit of trouble with the following C++11 code fragment and icpc 15.0.0. This example is compiled correctly by g++ 4.9. I essentially have a templated vector class (with its scalar type T and size N being template arguments).

The variadically templated constructor that checks whether all the arguments are assignable to T& is not found. When I remove the assignable check (which I cannot do because otherwise an ambiguity occurs with the copy-constructor when N=1) the example compiles.

The manually expanded version for 2 arguments compiles. Any idea for a work-around.

// > icpc -v
// icpc version 15.0.0 (gcc version 4.9.0 compatibility)

// > icpc -std=c++11 i_dont_know_what_is_wrong_with_intel.cpp
// i_dont_know_what_is_wrong_with_intel.cpp(39): error: no instance of constructor "Dummy<T, N>::Dummy [with T=double, N=2U]" matches the argument list
//             argument types are: (double, double)
//     Dummy<double, 2> t{1.0, 2.0};
//                       ^
// compilation aborted for i_dont_know_what_is_wrong_with_intel.cpp (code 2)

#include <type_traits>

template<typename ...T>
struct All : std::true_type { };

template<typename Head, typename ...Tail>
struct All<Head, Tail ...> : std::conditional<Head::value, All<Tail ...>, std::false_type>::type { };

template<typename T, unsigned N>
struct Dummy
{
  // this should work (and does in g++ 4.9), but doesn't
  template<typename ...Args, class = typename std::enable_if</*sizeof...(Args) == N && */
    All<
      std::is_assignable<T&, Args> ...>::value>::type>
  explicit Dummy(const Args &...args)
  {
  }
};

// this works
template<typename T>
struct DummyFixed2
{
  template<typename Arg1, typename Arg2, class = typename std::enable_if<
    All<
      std::is_assignable<T&, Arg1>,
      std::is_assignable<T&, Arg2>,>::value>::type>
  explicit DummyFixed2(const Arg1 &arg1, const Arg2 &arg2)
  {
  }
};

int main(int argc, char const *argv[])
{
  DummyFixed2<double> f{1.0, 2.0};
  Dummy<double, 2> t{1.0, 2.0};
  /* code */
  return 0;
}

 


Viewing all articles
Browse latest Browse all 2797

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>