c++ fatal error C1001 on using variadic template

Artem Zefirov

Hi everybody. Compiling my code I came upon the next error:
fatal error C1001: An internal error has occurred in the compiler.

When I pass only one parameter to "exec" function, everything is ok. But when I pass more then one parameters it issues an error. I use vs 2015 and visual c++ compiler respectively.

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
//using namespace std;

template <class T>
void param_push_(T arg, char ** param) {
  sprintf(*param, "%15.15e", arg);
}

template <class T, class... args>
void param_push_(T first, args... args, char ** param) {
  static int param_No = 0;
  sprintf(param[param_No++], "%15.15e", first);
  param_push(args..., param[param_No]);
}

template <class ... param_types>
void exec(const char * command, const param_types& ...param_values) {

  int arg_count = sizeof...(param_values);
  char ** params = new char*[arg_count];
  for (int i = 0; i < arg_count; ++i) {
    params[i] = new char[22 * sizeof(char)];
  }

  param_push_(param_values..., params); //cast parameters to (char *)  

  for (int i = 0; i < arg_count; ++i) {
    delete[] params[i];
  }
  delete[] params;

}

int main()
{
  double e_restriction = 0.55300000000000000000124124;
  double M_restriction = 5;

  exec("SELECT \"M\",e from orbital WHERE e < $1::double precision AND \"M\" < $2::double precision", e_restriction,M_restriction);

  return 0;
}
Adrian Colomitchi

Try this - it works.

#include <stdio.h>
#include <iostream>

template <typename ...> struct param_push;

template <class H, class... T>
struct param_push<H,T...> {
   void operator() (H head, T... tail, char** param) {
     sprintf(param[0], "%15.15e", head);
     param_push<T...> one_less;
     one_less(tail..., &param[1]);
   }
};

template <>
struct param_push<> {
  void operator() (char** param) {
    // does nothing, no more args to sprintf
  }
};

template <class ... param_types>
void exec(const char * command, const param_types& ...param_values) {

  int arg_count = sizeof...(param_values);
  char ** params = new char*[arg_count];
  for (int i = 0; i < arg_count; ++i) {
    params[i] = new char[22 * sizeof(char)];
  }

  // param_push_(param_values..., params); //cast parameters to (char *)
  struct param_push<param_types...> functor;
  functor(param_values..., params);

  for (int i = 0; i < arg_count; ++i) {
    std::cout << params[i] << " will be deleted" << std::endl;
    delete[] params[i];
  }
  delete[] params;

}

int main()
{
  double e_restriction = 0.55300000000000000000124124;
  double M_restriction = 5;

  exec("SELECT \"M\",e from orbital WHERE e < $1::double precision AND \"M\" < $2::double precision", e_restriction,M_restriction);

  return 0;
}

(and now, homework for Arty Zefirov: find the reason why when using template struct functors the technique works, why it fails for templated functions and update your question with the findings)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

fatal error C1001: internal error trying to use templates

From Dev

compiler error c1001 in generic class

From Dev

C++ variadic template delegation cycle error

From Dev

C++ variadic template delegation cycle error

From Dev

Error C1001 on Visual Studio 2015 with std::enable_if

From Dev

C++ Factory using variadic template issues

From Dev

Variadic template resolution in VS2013 - Error C3520

From Dev

ambiguous error : C++11 use variadic template multiple inheritance

From Dev

ambiguous error : C++11 use variadic template multiple inheritance

From Dev

C++ Variadic template - Unable to figure out the compilation error

From Dev

C++ Variadic Template Basics

From Dev

C++ specializing variadic template inside of a variadic template

From Dev

C++ passing template parameter pack from one variadic template to another causes compiler error

From Dev

C2780 error when using variadic function

From Dev

variadic variadic template template specialisation error

From Dev

Linker error for variadic template

From Dev

C++ Variadic template method specialization

From Dev

C++ bind member function with variadic template

From Dev

C++ - variadic template partial specialization

From Dev

C++ variadic template - limit number of args

From Dev

Wrap a function pointer in C++ with variadic template

From Dev

c++ template variadic function undefined reference

From Dev

C++11 variadic template + inheritance

From Dev

c++ variadic template constructor and common constructors

From Dev

C++ Variadic Template Remove Function Logic

From Dev

c++ variadic template argument iterating

From Dev

C++ Variadic Template Parameters to Linear Hierarchy

From Dev

c++ template variadic function undefined reference

From Dev

C++11 variadic template function storage

Related Related

  1. 1

    fatal error C1001: internal error trying to use templates

  2. 2

    compiler error c1001 in generic class

  3. 3

    C++ variadic template delegation cycle error

  4. 4

    C++ variadic template delegation cycle error

  5. 5

    Error C1001 on Visual Studio 2015 with std::enable_if

  6. 6

    C++ Factory using variadic template issues

  7. 7

    Variadic template resolution in VS2013 - Error C3520

  8. 8

    ambiguous error : C++11 use variadic template multiple inheritance

  9. 9

    ambiguous error : C++11 use variadic template multiple inheritance

  10. 10

    C++ Variadic template - Unable to figure out the compilation error

  11. 11

    C++ Variadic Template Basics

  12. 12

    C++ specializing variadic template inside of a variadic template

  13. 13

    C++ passing template parameter pack from one variadic template to another causes compiler error

  14. 14

    C2780 error when using variadic function

  15. 15

    variadic variadic template template specialisation error

  16. 16

    Linker error for variadic template

  17. 17

    C++ Variadic template method specialization

  18. 18

    C++ bind member function with variadic template

  19. 19

    C++ - variadic template partial specialization

  20. 20

    C++ variadic template - limit number of args

  21. 21

    Wrap a function pointer in C++ with variadic template

  22. 22

    c++ template variadic function undefined reference

  23. 23

    C++11 variadic template + inheritance

  24. 24

    c++ variadic template constructor and common constructors

  25. 25

    C++ Variadic Template Remove Function Logic

  26. 26

    c++ variadic template argument iterating

  27. 27

    C++ Variadic Template Parameters to Linear Hierarchy

  28. 28

    c++ template variadic function undefined reference

  29. 29

    C++11 variadic template function storage

HotTag

Archive