Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如果在使用constexpr函数作为参数时未在Visual Studio中工作,请启用__C++_Visual Studio_C++14_Constexpr_Enable If - Fatal编程技术网

C++ 如果在使用constexpr函数作为参数时未在Visual Studio中工作,请启用_

C++ 如果在使用constexpr函数作为参数时未在Visual Studio中工作,请启用_,c++,visual-studio,c++14,constexpr,enable-if,C++,Visual Studio,C++14,Constexpr,Enable If,我目前正在与Visual Studio 2017搏斗(使用/std:c++最新版本编译,如果有帮助的话) 所讨论的代码只是根据一些模板化的constexpr函数的结果选择一个结构专门化。GCC和clang在编译它时没有问题 这是我的MCVE: #include <type_traits> struct A { enum { test_trait = true }; }; template<typename T> constexpr int choos

我目前正在与Visual Studio 2017搏斗(使用
/std:c++最新版本编译,如果有帮助的话)

所讨论的代码只是根据一些模板化的
constexpr
函数的结果选择一个结构专门化。GCC和clang在编译它时没有问题

这是我的MCVE:

#include <type_traits>

struct A {
  enum {
    test_trait = true
   };
};

template<typename T>
constexpr int choose() {
  return T::test_trait;
}

template<typename T, typename Enable=void>
struct Chosen;

template<typename T>
struct Chosen<T, std::enable_if_t<choose<T>() == 1>> {};

void foo() {
  // This works
  constexpr int chosen = choose<A>();
  static_assert(chosen == 1, "");

  // This resolves to the undefined struct.
  using Chosen_t = Chosen<A>;
  Chosen_t x;
  (void)x;
}
但我真的不想跳过那个圈


模板解析应该能够以我期望的方式解决这个问题吗?我担心代码实际上是错的,GCC和clang只是对我宽容而已

MSVC 19.00.23506中的代码似乎仍然被破坏。但是,它似乎只需要一个更高级别的间接寻址(可能是更好的解决方法):

模板
结构ChosenImpl;
模板
结构ChosenImpl{};
模板
使用selected=ChosenImpl;


这样做的一个好处是,我们对调用者隐藏了第二个模板参数,而调用者根本不关心这一点。

这对VS 2015有效吗?这可能是一个与刚刚检查的@Justin相关的bug:不,visual studio 2015似乎也有相同的错误。VC++在模板方面的缺陷非常严重。代码完全正确。利用你的变通方法,今天就到此为止吧?@Columbo,如果我的代码不能以可移植的方式使用,我不会说它“很好”。(。但是,是的,我想很不幸,这将不得不这样做。
template<typename T> 
struct Chooser : public std::integral_constant<int, choose<T>()> {};

template<typename T>
struct Chosen<T, std::enable_if_t<Chooser<T>::value>> {};
template<typename T, bool>
struct ChosenImpl;

template<typename T>
struct ChosenImpl<T, true> {};

template<typename T>
using Chosen = ChosenImpl<T, choose<T>()>;