-2

Why is this syntax not allowed:

template<float&& value>
struct MyStruct{
  float GetValue() { return value; }
};

MyStruct<12.1f> myFloat;

And we have to instead do this:

template<const float& value>
struct MyStruct{
  float GetValue() { return value; }
};

extern constexpr float param = 12.1f;
MyStruct<param> myFloat;

I don't see any reason why the first should be forbidden.
The first syntax is far less clunky and the second syntax just makes it seem like the creators of c++ are just trying to make things difficult....So why is it like this?

DarthRubik
  • 3,927
  • 1
  • 18
  • 54
  • Because nobody wrote a proposal for this feature? – cpplearner Jun 04 '16 at 17:10
  • That monstrosity needs to be mangled. And matched to each other. I suspect that it will be easier to get `template class X;` through the committee than that stuff. – T.C. Jun 04 '16 at 20:36

1 Answers1

0

Because template processing is hugely complicated, I assume that the designers rightly rejected features that could be easily done using other means. Template parameters exist to customize the behavior of the code, not merely to insert constants. That's why int template parameters are supported, so you can do things like:

template <int SIZE> struct X {
    char buffer[SIZE];
};

If you just want a constant in your instance of the class, add it to the constructor:

class X {
public:
    X(float value_) {
        value = value_;
    }
    float value;
};

If you really want a customized class that uses that constant, how about:

class X71 : public X {
public:
    X71() : X(7.1f) {}
};
Wheezil
  • 3,157
  • 1
  • 23
  • 36
  • Yeah but template meta programming would be soooo much easier with types besides ints – DarthRubik Jun 04 '16 at 19:56
  • I think the idea moving forward is to encourage things like floating point compile time to be done with constexpr. TMP is usually for generic code, selecting paths at compile time, things you don't really use doubles for. – Nir Friedman Jun 04 '16 at 20:12