为什么Visual Studio不让我在enable_if中使用模板化的constexpr函数? [英] Why Won't Visual Studio let me use a Templatized, constexpr Function in enable_if?

查看:145
本文介绍了为什么Visual Studio不让我在enable_if中使用模板化的constexpr函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我将其简化为最小,完整,可验证的示例,并且似乎Visual Studio 2015不允许我使用模板化的 constexpr 函数在 enable_if 中。

So I've boiled this down to the minimal, complete, verifiable example and it seems that Visual Studio 2015 just won't allow me to use a templatized, constexpr function in an enable_if.

例如:

template <typename T>
constexpr bool condition() { return sizeof(T) > 1; }

给我错误:


错误C2995: enable_if< _Test,T> :: type test(void):函数模板已经定义

当我尝试将其用于替换失败时,不是这样的错误编译:

When I try to use it in substitution failure is not an error compilation like this:

template <typename T>
enable_if_t<condition<T>()> test() { cout << "true\n"; }

template <typename T>
enable_if_t<!condition<T>()> test() { cout << "false\n"; }

在gcc中可以正常使用: http://ideone.com/m9LDdS

如果我删除条件的模板化,则在Visual Studio 2015中可以正常工作。我相信constexpr 函数'c ++ 11' rel = tag> c ++ 11 ,为什么Visual Studio 2015不支持此功能?是虫子吗?

This works fine in gcc: http://ideone.com/m9LDdS
And it works fine in Visual Studio 2015 if I remove the templatization of condition. I believe that constexpr functions were introduced in c++11, why isn't Visual Studio 2015 supporting this? Is it a bug?

推荐答案

问题似乎是MSVC14 / VS2015无法结合constexpr函数的返回值正确解析SFINAE表达式作为模板参数。

The problem seems to be that MSVC14/VS2015 is not capable of correctly resolving SFINAE expressions in combination with return values of constexpr functions as template parameters.

作为解决方法,您可以将constexpr的返回值分配给结构的静态const成员,然后将该成员用作模板参数。 / p>

As a workaround you can assign the return value of your constexpr to a 'static const' member of a struct and use this member as template parameter.

#include <type_traits>
#include <iostream>

using std::enable_if_t;
using std::cout;

template <typename T>
constexpr bool condition() { return sizeof(T) > 1; }

template <typename T>
struct condition_ { static const bool value = condition<T>();};

template <typename T>
enable_if_t<condition_<T>::value> test() { cout << "true\n"; }
template <typename T>
enable_if_t<!condition_<T>::value> test() { cout << "false\n"; }

int main() {
    test<int>();
    test<bool>();
    return 0;
}

http://rextester.com/VVNHB62598

评论,您的实际问题出现在不同于MCVE的情况下(如何初始化div_t对象?

You also mentioned in the comments that your actual problem appeared in another case than your MCVE (How can I Initialize a div_t Object?)

在这种情况下,解决方法如下:

For this case the workaround might look like this:

#include <type_traits>
#include <cstdlib>
#include <utility>


template <typename T>
using divtype = decltype(std::div(std::declval<T>(), std::declval<T>()));


template <typename T>
struct condition
{
    static const bool value = divtype<T>{ 1, 0 }.quot != 0;
};


template <typename T>
std::enable_if_t<condition<T>::value, divtype<T>> make_div(const T quot, const T rem) { return{ quot, rem }; }

template <typename T>
std::enable_if_t<!condition<T>::value, divtype<T>> make_div(const T quot, const T rem) { return{ rem, quot }; }

int main() {

    make_div<int>(1, 2);
    return 0;
}

http://rextester.com/ULDFM22040

根据此 Visual Studio C ++ Team博客条目 VS2015不(完全)支持表达式SFINAE

According to this Visual Studio C++ Team blog entry VS2015 does not have (complete) support for Expression SFINAE yet.


[1]我们计划在2015年之后立即在编译器中实现Expression SFINAE。 RTM,我们计划在2015年的更新中提供它,以支持生产用途。 (但不一定是2015 Update1。它可能需要更长的时间。)

[1] We’re planning to start implementing Expression SFINAE in the compiler immediately after 2015 RTM, and we’re planning to deliver it in an Update to 2015, supported for production use. (But not necessarily 2015 Update 1. It might take longer.)

这篇关于为什么Visual Studio不让我在enable_if中使用模板化的constexpr函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆