C++17 changes N4268 Allowing more non-type template args

Ed Kurlyak 21 Reputation points
2024-09-27T17:21:31.7933333+00:00

I have VS2019 ver16.11.40 (updated VS).

I have sample C++ code accroding to C++17 changes Allowing more non-type template args:

#include <iostream>
 
template<double Value>
constexpr double getFactorial()
{
    return Value * getFactorial<Value - 1.0>();
}
 
template<>
constexpr double getFactorial<0.0>() { return 1.0; }
 
int main()
{
    std::cout << getFactorial<4.0>() << '\n';
}

This code doesn't want to compile on mine VS2019 ver.16.11.40 despite document:

link

says this C++17 change applied to VS 2017 15.5, see screenshot below:

image C++17 change

What the matter? Why this C++17 code doesnt want compiles on my VS2019 v.16.11.40?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,719 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 117.1K Reputation points
    2024-09-27T17:54:30.9766667+00:00

    It seems that double was not yet supported, but it is possible to use long or long long:

    template<long Value>
    constexpr long getFactorial( )
    {
        return Value * getFactorial<Value - 1>( );
    }
    
    template<>
    constexpr long getFactorial<0>( ) { return 1; }
    

    To use double, consider C++20 and Visual Studio 2022 (https://en.cppreference.com/w/cpp/language/template_parameters).

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Ed Kurlyak 21 Reputation points
    2024-09-28T07:15:33.2933333+00:00

    Thank you very much for answer. I read this document C++17:

    https://en.cppreference.com/w/cpp/compiler_support/17

    Code from example describes C++17 changes, but code same doesnt not compiled on my VS2019 ver16.11.40.

    #include <iostream>
    template<int* p> class X { };
    
    int a[10];
    
    struct S { int m; static int s; } s;
    
    int main()
    {
        X<&a[2]> x3; // before C++17 error: address of array element
        X<&s.m> x4;  // before C++17 error: address of non-static member
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.