<pre name="code" class="plain">
// cascadederivationreturntypeofcurrentderivationlevel.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
template <class T>
class A
{
public:
// 以下代码C2355
// 1 try
//static auto Empty()->decltype(*this) // C2355 decltype(*this) 在这个类中还没有定义
//{
// this->typeid l; // C2355 decltype(*this) 在这个类中还没有定义
// return l;
//}
// 2 try
//static auto Empty()->decltype(*this) // C2355 decltype(*this) 在这个类中还没有定义
//{
// decltype(*this) l; // C2355 decltype(*this) 在这个类中还没有定义
// return l;
//}
// 3 try
//auto Empty()->decltype(*this) // C2355 decltype(*this) 在这个类中还没有定义
//{
// return *this;
//}
// 以下代码在某种程度上可以运行
//template <class T>
//static auto Empty()->decltype(*this)
//{
// this->typeid<T> l;
// return l;
//}
// 原因:虽然 this 能够被运行时推断因此可以被认为已知,但是 this 的类型不能够被运行时推断因此不能够被认为已知,所以 this (的类型)被认为没有定义
// 以下代码可行
// 1 okay
//template <class T> // 函数模板
//static c Empty()
//{
// T l;
// return l;
//}
};
template <class T>
class B : public A<T>
{
public:
// 以下代码可行
// 2 okay
static B<T> Empty()
{
B<T> l;
return l;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
// 以下代码C2355
// 1&2 try
//B<int> b = B<int>::Empty();
// 3 try
//B<int> type;
//B<int> b = type.Empty();
// 以下代码可行
// 1 okay
//B<int> b = B<int>::Empty<B<int>>();
// 2 okay
B<int> b = B<int>::Empty();
return 0;
}