【问题标题】:Unexplained Error in Visual studio c++ compilerVisual Studio C++ 编译器中出现无法解释的错误
【发布时间】:2021-11-11 01:26:41
【问题描述】:

您好,我遇到以下代码的问题; 在这一行

constexpr static Test<T> Reset = Test<T>();

错误 C2027:使用未定义类型 NS::Test

但是我在 Linux gcc 中编译这些代码没有问题。如何在 Windows VS C++ 中构建这些代码?

#include <type_traits>
#include <cstdint>
#include <string>

namespace NS
{
    template <class T>
    class Test
    {
    public:
        constexpr static Test<T> Reset = Test<T>();

    private: 
    };

    template <class T>
    constexpr Test<T> Test<T>::Reset;
} 

struct Def
{
    static constexpr uint16_t Init = 0xffff;
    
};

using  MY = NS::Test<Def>;

struct TransactionInfo
{
    MY          my;
};

【问题讨论】:

标签: c++ visual-studio gcc


【解决方案1】:

一个简单的解决方法是让Reset 成为一个函数而不是一个值。

模板类型的成员函数是“按需”定义的,这会将罐子踢到调用站点,我们知道Test&lt;T&gt; 将完成。

namespace NS
{
    template <class T>
    class Test
    {
    public:
        constexpr static Test<T> Reset() {
          return {};
        }

    private: 
    };

} 

【讨论】:

  • 老实说,我很惊讶 G++ 一开始就让它飞起来。 Test&lt;T&gt; 在定义点上显然是一个不完整的类型。 From the standard: "内联静态数据成员的声明(这是一个定义)..."
猜你喜欢
  • 2021-02-22
  • 1970-01-01
  • 2010-10-09
  • 2014-09-15
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 2016-04-22
  • 1970-01-01
相关资源
最近更新 更多