繁体   English   中英

MSVC 中可能的编译器错误

[英]Possible compiler bug in MSVC

以下代码使用 gcc 和 clang(以及许多其他 C++11 编译器)进行编译

#include <stdint.h>

typedef int datatype;

template <typename T>
struct to_datatype {};

template <>
struct to_datatype<int16_t> {
  static constexpr datatype value = 1;
};

template <typename T>
class data {
 public:
  data(datatype dt = to_datatype<T>::value) {}
};

int main() {
  data<char> d{to_datatype<int16_t>::value};
}

使用(几乎)最新的 MSVC 编译时

> cl .\test.cpp /std:c++latest /permissive-
Microsoft (R) C/C++ Optimizing Compiler Version 19.24.28314 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

test.cpp
.\test.cpp(16): error C2039: 'value': is not a member of 'to_datatype<T>'
        with
        [
            T=char
        ]
.\test.cpp(16): note: see declaration of 'to_datatype<T>'
        with
        [
            T=char
        ]
.\test.cpp(20): note: see reference to class template instantiation 'data<char>' being compiled

这是MSVC的错误吗? 如果是,C++ 标准中的哪个术语最能描述它?

如果您将部分代码替换为

template <typename T>
class data {
 public:
  data(datatype dt) {}
  data() : data(to_datatype<T>::value) {}
};

无论如何它编译顺利。

我会说 MSVC 不接受代码是错误的。

根据 C++17 标准最终草案的[dcl.fct.default]/5 ,类模板的成员函数的默认参数中的名称查找是根据 [temp.inst] 中的规则完成的。

根据[temp.inst]/2类模板的隐式实例化不会导致成员函数的默认参数的实例化,并且根据[temp.inst]/4的成员函数的默认参数(非显式特化a) 类模板在被调用使用时被实例化。

在您的代码中没有使用默认参数to_datatype<T>::value调用,因此不应实例化它。 因此,在to_datatype<char>查找value不应该出现错误。

(C++11 标准最终草案中的相关部分具有等效的措辞,但编号除外,请参见[decl.fct.default]/5[temp.inst]/1[temp.inst]/3 。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM