考虑以下C++程序:
#include <iostream>
template<typename T>
class A
{
public:
explicit A(T& x) : x_(x){}
const T& get() { return x_; }
private:
T x_;
};
int main()
{
int x = 42;
A<int&>(x).get() = 43; // compiles fine, even though get() looks like it returns a const ref
std::cout << x << '\n';
}
Run Code Online (Sandbox Code Playgroud)
程序编译OK并输出43.这表明get()返回的看似const引用实际上是一个非const引用,因为它允许修改它引用的值.
是引用此行为的参考规则崩溃吗?
如何强制从get()返回的引用行为类似于const引用,也就是说,它不允许修改它引用的值?
考虑一个基于可变参数模板的别名,它在扩展表达式中使用它的第一个元素:
template<typename T, typename... Ts>
using AllSame = std::conjunction<std::is_same<T, Ts>...>;
Run Code Online (Sandbox Code Playgroud)
如果我们通过单独传递第一个元素来使用此别名,则代码编译良好:
template<typename T, typename... Ts>
using X = AllSame<T, Ts...>;
Run Code Online (Sandbox Code Playgroud)
但简单地传递整个包并不能编译:
template<typename... Ts>
using X = AllSame<Ts...>;
Run Code Online (Sandbox Code Playgroud)
这是编译错误:
error: pack expansion argument for non-pack parameter 'T' of alias template 'template<class T, class ... Ts> using AllSame = typename std::enable_if<std::is_same<bool_pack<true, std::is_same<T, Ts>::value ...>, bool_pack<std::is_same<T, Ts>::value ..., true> >::value>::type'
Run Code Online (Sandbox Code Playgroud)
为什么调用站点要单独传入第一个参数,而不是直接传入整个包?
CRTP模式中的基类可以访问派生类的成员函数,但它无法访问派生类中的嵌套类型.
为何如此区别?
为了说明,请考虑以下代码:
template<typename Derived>
struct crtp_base
{
void crtp_method() { return static_cast<Derived&>(*this).method(); } // compiles
using crtp_type = typename Derived::type; // doesn't compile
};
struct X : public crtp_base<X>
{
void method() {}
using type = int;
};
int main()
{
}
Run Code Online (Sandbox Code Playgroud)
crtp_type导致编译错误,虽然crtp_method编译都很好,但两者都试图访问Derived类中定义的内容.什么是解释这种差异的C++规范?
考虑以下代码:
[1,2,3].map.with_index { |x, i| x * i }
# => [0,2,6]
Run Code Online (Sandbox Code Playgroud)
这到底是如何运作的?
我的思维模型map是它迭代并在每个元素上应用一个函数。是否with_index以某种方式将函数传递给 enumerator [1,2,3].map,在这种情况下该函数会是什么?
这个SO 线程显示了枚举器如何传递数据,但没有回答问题。事实上,如果你替换map为each那么行为是不同的:
[1,2,3].each.with_index { |x, i| x * i }
# => [1,2,3]
Run Code Online (Sandbox Code Playgroud)
map除了携带要迭代的数据之外,似乎还携带必须应用函数的信息。这是如何运作的?