samedi 3 décembre 2016

Type of std::normal_distribution depending on template

Rarely T can be int and it will hang at:

std::normal_distribution<T> distribution(0.0, 0.1);

This will be used in the next lines of my function and I wouldn't like to have an if/else with all the lines of code that depend on that. As a result I am trying to do this:

#include <random>

#define T int

int main ()
{
  std::normal_distribution<((std::is_same<T, int>::value) ? (float) : (T))> distribution (0.0, 1$

  return 0;
}

and I am getting these errors:

Georgioss-MacBook-Pro:Code gsamaras$ g++ -std=c++0x main.cpp 
main.cpp:7:69: error: expected expression
  std::normal_distribution<((std::is_same<T, int>::value) ? (float) : (T))> distribution ...
                                                                    ^
main.cpp:7:74: error: expected expression
  std::normal_distribution<((std::is_same<T, int>::value) ? (float) : (T))> distribution ...
                                                                         ^
main.cpp:7:77: error: C++ requires a type specifier for all declarations
  std::normal_distribution<((std::is_same<T, int>::value) ? (float) : (T))> distribution ...
                                                                            ^
3 errors generated.

Any idea on how to fix this or an alternative?

Aucun commentaire:

Enregistrer un commentaire