1

I don't really know how to rename the function below. The example only works with template specialization.

    template<typename A, typename B>
    auto MAX(const A &one, const B &two) -> decltype(one > two ? one : two) {
        static_assert(std::is_floating_point<A>::value || std::is_integral<A>::value, "ERROR - bigger(): template parameters not of type float or int\n");
        static_assert(std::is_floating_point<B>::value || std::is_integral<B>::value, "ERROR - bigger(): template parameters not of type float or int\n"); 

        return one > two ? one : two;
    }

    template<typename A, typename B>
    const auto min = &MAX<A,B>;

Works:

    const auto min = &MAX<float,float>;
Praetorian
  • 106,671
  • 19
  • 240
  • 328
dgrat
  • 2,214
  • 4
  • 24
  • 46
  • 1
  • "This doesn't work" is useless without what error messages you are getting. Also useful is what compiler you get them from. – Yakk - Adam Nevraumont Jan 05 '16 at 18:56
  • I do not think that your `decltype()` expression will work as expected because the values of `one` and `two` (barring some rare cases with `constexpr`) cannot be known at compile time. – Joel Cornett Jan 05 '16 at 19:11
  • Also, it does not make sense to name a function that calculates the maximum `min`. Is that a typo? – Joel Cornett Jan 05 '16 at 19:24
  • The name is just a dumb example. Is there a Cxx11 way to solve this? – dgrat Jan 05 '16 at 19:43
  • @JoelCornett An expression doesn't need to be evaluated in order to figure out its type. `decltype` very specifically does not evaluate its operand, just like `sizeof`. – Igor Tandetnik Jan 06 '16 at 06:46
  • 1
    `template auto min(const A &one, const B &two) -> decltype(MAX(one, two)) { return MAX(one, two); }` – Igor Tandetnik Jan 06 '16 at 06:48
  • take a look at : http://stackoverflow.com/questions/13698989/what-is-the-best-way-of-renaming-alias-forward-a-function-in-c – Garf365 Jan 06 '16 at 13:51
  • @IgorTandetnik: I didn't say that it wouldn't compile. I said it wouldn't work as expected because there is no way the order operation could be resolved at compile time. There is no way the resolved type would be correct. – Joel Cornett Jan 06 '16 at 16:33
  • @JoelCornett What is the "correct" type that you expect? Do you believe that the type of the expression `one > two ? one : two` depends on whether `one` is actually greater than `two`, and therefore may change at runtime? This is, of course, not the case - C++ is a statically typed language, types of all expressions are known at compile time. Do you also believe that the value of `sizeof(one > two ? one : two)` is determined at runtime? Try it, see for yourself. – Igor Tandetnik Jan 06 '16 at 20:23
  • @IgorTandetnik The expression as it stands doesn't make sense *because* the type will be determined at compile time. There's no reason for the expression in the first place -- it won't do what it "claims" to do. – Joel Cornett Jan 06 '16 at 21:36
  • @JoelCornett What do you believe the clause claims to do, and what do you believe it does instead? I don't at all understand the nature of your objection. As far as I can tell, the clause says "the return type of this function is the type that normal coercion rules for `?:` operator would produce given operands of types `A` and `B`". Do you read the clause as claiming something else, or do you believe that it does claim this, but doesn't actually deliver? – Igor Tandetnik Jan 06 '16 at 21:57

0 Answers0