次のコードでコンパイルエラーが発生しています。一時オブジェクトでもconst参照にバインドできると期待しています。だから私はそれが有効なコードであるべきだと思っています。ただし、g ++ではこのエラーが発生しますが、clangではこのようなエラーは発生しません。誰かが私にこれが起こる正確な理由を教えてくれますか?
#include <iostream>
struct TestClass
{
TestClass() : str()
{
strncpy(str, "hello", sizeof(str));
}
char str[6];
char (&getStr())[6]
{
return str;
}
};
template <typename T>
void printFunc(const T& str)
{
std::cout << str << std::endl;
}
int main()
{
TestClass obj;
printFunc(obj.str);
// printFunc(TestClass().str); // <- This line gives compilation error.
printFunc(TestClass().getStr());
return 0;
};