【问题标题】:Inconsistent typeid for nested template function in Visual StudioVisual Studio 中嵌套模板函数的类型 ID 不一致
【发布时间】:2015-04-17 15:45:49
【问题描述】:

我在使用 Visual Studio 2013 Community 和 2013 年 11 月 CTP 进行编译时偶然发现了我的程序中的一个奇怪行为。以下程序编译并打印“true”,而预期的行为是它会打印“false”,这是 GCC and clang 所做的。

我已经在我的设置以及以下站点上测试了此代码:http://webcompiler.cloudapp.net/(声称 VS 编译器版本 19,也打印“true”)、http://codepad.org/http://www.tutorialspoint.com/compile_cpp_online.php 和其他一些站点。

我不确定这里的正确行为是什么,或者下面的代码是否实际上是正确的 C++ 代码,所以我很困惑。如果有人能对这里发生的事情有所了解,那就太好了。

#include <stdio.h>
#include <typeinfo>

template <typename T>
struct foo
{
    template <typename U>
    static void bar() {}
};

template <typename T>
void baz() {
    puts(typeid(&foo<T>::template bar<int>) == typeid(int) ? "true" : "false");
}

int main() {
    baz<double>();
}

编辑

感谢 Reddit,我设法让 STL 注意到了这个错误,STL 说他已经报告了这个问题,并将在 VS 2015 RTM 中修复:http://www.reddit.com/r/cpp/comments/2zs2ob/vc_2015_rtm_what_do_you_want_it_to_have/cpm01wr

【问题讨论】:

  • 更正了您的包含并进行了一些清理和格式化。使用您的编译器是否仍然显示错误? coliru.stacked-crooked.com/a/1a04edd5a5b40d19
  • 那么您提交错误报告了吗?如果是这样,您能否在此处链接到它,以便我们对其进行投票?
  • 很遗憾,我收到“您无权提交此连接的反馈”。当我尝试使用 Visual Studio Connect 报告错误时出错。

标签: c++ visual-studio templates visual-c++


【解决方案1】:

VS2013(用 VS2013.4 测试)确实错误地将成员函数模板的类型(static 或不)作为int。考虑下面的简化示例,其中代码是正确的 C++ 应该非常明显:

#include <typeinfo>
#include <iostream>

struct foo
{
    template<typename T>
    static void bar()
    { }
};

int main() {
    std::cout << typeid(&foo::bar<int>).name() << "\n";
    std::cout << typeid(int).name() << "\n";
    std::cout << (typeid(&foo::bar<int>) == typeid(int) ? "true\n" : "false\n");
}

哪个会打印

int
int
true

而不是删除bar的模板参数时发生的情况:

void (__cdecl*)(void)
int
false

【讨论】:

    猜你喜欢
    • 2019-07-23
    • 2021-01-23
    • 2022-07-19
    • 1970-01-01
    • 2015-07-03
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    相关资源
    最近更新 更多