我有以下C++ 11代码.
#include <type_traits>
using IntType = unsigned long long;
template <IntType N> struct Int {};
template <class T>
struct is_int : std::false_type {};
template <long long N>
struct is_int<Int<N>> : std::true_type {};
int main()
{
static_assert (is_int<Int<0>>::value, "");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Clang ++ 3.3编译代码,但在g ++ 4.8.2静态断言失败
$ g++ -std=c++11 main.cpp
main.cpp: In function ‘int main()’:
main.cpp:15:5: error: static assertion failed:
static_assert (is_int<Int<0>>::value, "");
^
$
Run Code Online (Sandbox Code Playgroud)
问题是由不同的积分模板参数引起的.在这种情况下哪个编译器是正确的?
我有一个与Rust问题#34284相关的问题.我理解为什么在当前的Rust中不可能,但我很好奇需要什么样的(破坏性)更改来避免分配Cow::clone
.
我怀疑它需要某种终身专业化.像(伪Rust)的东西:
impl<'a, B: ?Sized> Clone for Cow<'a, B> where B: ToOwned {
fn clone(&'b self) -> Cow<'a, B> {
if /*lifetimes 'a and 'b allow for optimization*/ {
return /*shallow copy*/
} else {
return /*as it is today*/
}
}
}
Run Code Online (Sandbox Code Playgroud)
RFC 1210声明基于生命期的专业化是不可能的:
我们不能,因为当编译器实际生成代码("trans")时,生命周期信息已被删除
但后来它说了
不幸的是,我们不能轻易地排除不受欢迎的依赖于生命的特化,因为它们可以"隐藏"在看似无辜的特质界限之后
这是否意味着:
Cow::clone
如果不更改编译器就无法进行优化,以便更长时间地保留生命周期信息?'static
如果RFC 1210允许,它可以仅针对语言定义的生命周期()进行优化吗?