c++ - char* 和 char[N] 的模糊错误

标签 c++ templates

谁能帮助我了解这种行为是否正确。

考虑这个例子:

#include <iostream>
using namespace std;

template <typename T>
struct test {
};

template <typename T>
bool operator==(const test<T>& obj, const T* arr) {
    return true;
}

template <typename T, size_t TN>
bool operator==(const test<T>& obj, const T (&arr)[TN]) {
    return false;
}

int main() {
    cout << ( test<char>() == "string" ) <<endl;
    return 0;
}

使用 gcc 4.7.3,它编译得很好,并按预期输出“0”。

但是对于 Visual Studio 编译器,它会报告一个不明确的错误 (C2593)

在这种情况下谁是对的?标准对此有何看法?

谢谢。

最佳答案

使用 gcc 的最新版本(即开发分支的最新负责人)和 clang也表现出歧义。我本以为采用数组的重载更好,但代码似乎模棱两可。不过,我还没有找到标准中的相关条款。

关于c++ - char* 和 char[N] 的模糊错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20747180/

相关文章:

c++ - Visual Studio 2015 中的 "non-standard syntax; use ' 和 ' to create a pointer to member"错误

c++ - 如何根据某些值删除/删除类对象的 vector

c++ - 我需要帮助修复错误消息 - "error LNK2019: unresolved external symbol"

css - 非 Joomla 网站的 Joomla 模板

c++ - 引用 vector 的部分片段?

c++ - 对前向声明感到困惑

c++ - 在可变参数模板中扩展变量名称

ios - Cocos2d v2.x,无法安装模板。权限被拒绝。我在做傻事吗?

C++ 如何在类中缓存模板类型 T 的变量?

c++ - 当通过引用访问时,Constexpr 成员函数返回不被视为 constexpr 的模板参数