在clang的C++ 11状态页面中遇到了一个名为"rvalue reference for*this"的提案.
我已经阅读了很多关于rvalue引用并理解它们的内容,但我认为我不知道这一点.我也无法使用这些条款在网上找到太多资源.
页面上的提案文件有一个链接:N2439(将移动语义扩展到*this),但我也没有从中获得太多的例子.
这个功能是什么?
我知道语言规范禁止功能模板的部分特化.
我想知道为什么禁止它的理由?它们没用吗?
template<typename T, typename U> void f() {} //allowed!
template<> void f<int, char>() {} //allowed!
template<typename T> void f<char, T>() {} //not allowed!
template<typename T> void f<T, int>() {} //not allowed!
Run Code Online (Sandbox Code Playgroud) c++ language-design partial-specialization template-specialization function-templates
在没有'右值引用*this'功能的变通方法中,我看到以下成员函数(转换运算符):
template< class T >
struct A
{
operator T&&() && // <-- What does the second '&&' mean?
{
// ...
}
};
Run Code Online (Sandbox Code Playgroud)
第二对&&是什么意思?我不熟悉那种语法.
有谁知道,在C++ 11中,函数模板是否可以部分专用?
我有一个带有类型和非类型模板参数的模板类.我想专门化一个成员函数,我发现的是,如下例所示,我可以完全专业化.
template<typename T, int R>
struct foo
{
foo(const T& v) :
value_(v)
{}
void bar()
{
std::cout << "Generic" << std::endl;
for (int i = 0; i < R; ++i)
std::cout << value_ << std::endl;
}
T value_;
};
template<>
void foo<float, 3>::bar()
{
std::cout << "Float" << std::endl;
for (int i = 0; i < 3; ++i)
std::cout << value_ << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
但是这种部分特化不会编译.
template<int R>
void foo<double, R>::bar()
{
std::cout << "Double" << std::endl;
for (int i …Run Code Online (Sandbox Code Playgroud) c++ templates partial-specialization template-specialization
在下面的代码段中,
template<typename T1>
void func(T1& t)
{
cout << "all" << endl;
}
template<typename T2>
void func(T2 &t)
{
cout << "float" << endl;
}
// I do not want this
// template<> void func(float &t)
int main()
{
int i; float f;
func(i); // should print "all"
func(f); // should print "float"
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望修改模板,通过传递浮动以外的任何类型将打印"all"并传递float将打印"float".我不想要模板特化,而是根据输入类型进行相应的部分特化.我应该怎么做呢.提前致谢.
那个场景,我现在面临的是,我需要定义以下内容,
template<typename T1>
void func(T1 &t)
{
cout << "t1" << endl;
}
template<typename T2>
void func(T2 &t)
{
cout << "t2" << …Run Code Online (Sandbox Code Playgroud)