我有以下代码在g ++下编译,但不是用clang编译.
如果以各种次要方式进行更改,Clang将编译代码,例如合并2个名称空间声明.
// The problem disappears without namespaces.
namespace Root {
// The problem disappears if 'g' is in the global namespace, and we change
// the friend declaration to '::g'
// The problem disappears if 'g' has void return type.
// The problem disappears if we get rid of the 'Value' template argument
// and the 'value' parameter.
template<typename Value, typename Defaulted = void>
bool g(Value value);
// The problem disappears if MyClass is not a template.
template<typename …Run Code Online (Sandbox Code Playgroud) 我已经删除了一些在Visual Studio 2015上无法编译的C++ 11代码到我认为应该编译的以下代码(并且使用clang和gcc):
#include <utility>
void test(const char* x);
int main()
{
const char x[] = "Hello world!";
test(std::forward<const char*>(x));
}
Run Code Online (Sandbox Code Playgroud)
我理解forward这里没有必要打电话.这是从一个更复杂的代码中减少的,它将可变参数中的任何数组衰减到指针并转发所有内容.我确信可以通过模板专业化或SFINAE找到解决这个问题的方法,但是在我走这条路之前,我想知道它是否是有效的C++.编译器是Visual Studio 2015,可以在此在线MSVC编译器上重新创建问题.编译错误是:
main.cpp(13): error C2665: 'std::forward': none of the 2 overloads could convert all the argument types
c:\tools_root\cl\inc\type_traits(1238): note: could be '_Ty &&std::forward<const char*>(const char *&&) noexcept'
with
[
_Ty=const char *
]
c:\tools_root\cl\inc\type_traits(1231): note: or '_Ty &&std::forward<const char*>(const char *&) noexcept'
with
[
_Ty=const char *
]
main.cpp(13): note: while trying to …Run Code Online (Sandbox Code Playgroud) c++ compiler-errors language-lawyer c++11 visual-studio-2015