首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >std::使用msvc,但不是在VS2019中使用Godbolt中的条件编译

std::使用msvc,但不是在VS2019中使用Godbolt中的条件编译
EN

Stack Overflow用户
提问于 2021-06-18 09:55:50
回答 1查看 64关注 0票数 1

我有这样的代码:

代码语言:javascript
复制
#include<type_traits>

enum class DataFormat : int8_t
{
    cDouble = 0,
    cFloat  = 1,
    cChar   = 2,
    cBool   = 3
};

class MatrixBase
{
protected:
    MatrixBase(DataFormat const aDataFormat) : _dataFormat(aDataFormat) {}

public:
    DataFormat getFormat() const { return _dataFormat; }

private:
    DataFormat _dataFormat;
};

template<typename tType>
class Matrix final : public MatrixBase
{
private:
    struct DataFormatBool { static constexpr DataFormat _csValue = DataFormat::cBool; };
    struct DataFormatFloat { static constexpr DataFormat _csValue = DataFormat::cFloat; };
    struct DataFormatDouble { static constexpr DataFormat _csValue = DataFormat::cDouble; };
    using ActualDataFormat = typename std::conditional<std::is_same<tType, bool>::value, DataFormatBool,
        std::conditional < std::is_same<tType, float>::value, DataFormatFloat, DataFormatDouble>>::type;
    static_assert(std::is_same<tType, bool>::value || std::is_same<tType, float>::value || std::is_same<tType, double>::value, "Matrix supports only bool, float or double.");

public:
    Matrix()
        : MatrixBase(ActualDataFormat::_csValue)
        
    {  }
};

DataFormat rrr() {
    Matrix<bool> m;
    return m.getFormat();
}

它在哥德波特下编译,但在VS2019下不编译。(在那里它更大。)错误是

模型-stl\include\matrix.h(46,52):error C2039:'_csValue':不是'std::conditional‘的成员。

我做错什么了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-18 10:28:22

rrr的声明更改为

代码语言:javascript
复制
Matrix<float> m;

在所有编译器上导致编译失败:

缺少从内部std::conditional提取结果类型。这应该是:

代码语言:javascript
复制
using ActualDataFormat =
    typename std::conditional<std::is_same<tType, bool>::value,
                  DataFormatBool,
                  typename std::conditional<
                      std::is_same<tType, float>::value,
                      DataFormatFloat,
                      DataFormatDouble>::type
                  >::type;
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68033075

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档