Condzi
Condzi

Reputation: 23

if constexpr and syntax error 'type'

I'm making function that converts ints or floats to string:

#include <functional>
#include <string>
#include <iostream>

template <typename From>
inline std::string ToStr( const From& src )
{
    constexpr bool isIntegral = std::is_integral<From>::value;
    constexpr bool isFloat = std::is_floating_point<From>::value;

    if constexpr ( isIntegral )
        return ( std::_Integral_to_string<char>( src ) );
    else if ( isFloat )
        return ( std::_Floating_to_string( "%f", src ) );
}

int main()
{
    std::cout << ToStr( 123 );
}

I'm using Visual Studio 2017 and I get following errors:
- syntax error 'type' on line 11 (first if)
- illegal else without matching if on line 13 (second if)

I don't know how to fix it, any ideas?

Upvotes: 2

Views: 1190

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

You're running version 15.2.

Microsoft said in a blog post that if constexpr is supported in 15.3 (Preview 2).

So, try upgrading Visual Studio.

Microsoft makes it very difficult to clearly manage different versions of their software, because they want everyone to "just" upgrade to the latest all the time. Sadly that is not particularly practical in cases like this, when you need to know what is in what.

Upvotes: 4

Related Questions