我们在编译下面的源代码时发现了一个奇怪的行为:
template<template<class> class TT> struct X { };
template<class> struct Y { };
template<class T> using Z = Y<T>;
int main() {
X<Y> y;
X<Z> z;
z = y; // it fails here
}
Run Code Online (Sandbox Code Playgroud)
这是一个略微修改的示例,取自c ++ 11标准模板别名提案:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2258.pdf(参见第4页) )另请注意,该提案"声明y和z属于同一类型".因此,在我们的解释中,应该可以从y指定(或复制构造)z.
但是,此代码不使用gcc 4.8.1编译,也不使用clang 3.3编译.这是编译器中的错误还是我们误解了标准?
提前谢谢,craffael等;)
PS Clang错误消息是:
error: no viable overloaded '='
note: candidate function (the implicit copy assignment operator) not viable: no known conversion from 'X<template Y>' to 'const X<template Z>' for 1st argument
template<template<class> class TT> struct …Run Code Online (Sandbox Code Playgroud) 最近,当我尝试优化自己的包含层次结构时,偶然发现了该文件a.hpp:
template<class T>
class A
{
using t = typename T::a_t;
};
class B;
extern template class A<B>;
Run Code Online (Sandbox Code Playgroud)
这似乎是错误的形式。实际上,似乎extern模板语句的末尾似乎导致实例化A<B>,导致编译器抱怨类型不完整。
我的目标将是确定A<B>的a.cpp:
#include <b.hpp>
template class A<B>;
Run Code Online (Sandbox Code Playgroud)
这样,我避免必须包括b.hpp来自a.hpp这似乎是一个好主意,以减少编译时间。但是,它不起作用(a.hpp本身无法编译!)是否有更好的方法呢?
注意:当然,我不能只使用显式模板实例化,但这不是我想要的!我想“预编译” A<B>以节省编译时间(如果已使用),但如果A<B>未使用,我不想b.hpp在每个使用a.hpp!的文件中都包括在内!