class Parent {};
class Child : public Parent {};
class Foo
{
public:
Foo (Parent &) {};
template <typename T>
Foo (const T &);
};
int main ()
{
Child c;
Foo foo (c);
}
这会产生一个链接器错误,因为foo的构造函数选择template<typename T>Foo::Foo(const T &)而不是Foo::Foo(Parent&
在处理一个项目时,我遇到了std::apply不转发来自std::tuple的右值引用的情况,该引用是由std::forward_as_tuple *IF* resulting std::tuple创建的,存储在一个变量中!然而,如果std::forward_as_tuple结果没有存储在变量中,而只是作为第二个参数传递给std::apply,那么它就会正常工作,并且右值引用会得到完美的转发。 我尝试了许多方法,包括对std::tuple使用不同的类型,例如 decltype(auto) t = forward_as_tuple(1, std::move(r))
auto t = forwa
是否可以传递一个struct并只读到一个函数?(就像T const& in C++)
struct A
{
public int b;
}
void func1(ref A a) // I want to make this `a` immutable
{
}
怎么做?
更新
我最关心的是有效地传递(1)不变状态(2)。第二,需要注意的是变异状态必须简单易用,就像可变对象一样。
在我的C++旅行中,我遇到了以下成语(例如,中的Abseil),用于确保模板函数不能显式指定模板参数,因此它们不是保证API的一部分,可以自由更改而不破坏任何人:
template <int&... ExplicitArgumentBarrier, typename T>
void AcceptSomeReference(const T&);
它似乎确实奏效了:
foo.cc:5:3: error: no matching function for call to 'AcceptSomeReference'
AcceptSomeReference
创建多维数组im时会出现以下错误:
a non static member reference must be relative to a specific object c++
我的代码:
class Level
{
private:
int width = 6;
int height = 6;
Room rooms[width][height];
public:
Level();
~Level();
};
当宽度和高度被替换时,当创建数组时,用6(int‘s的值)来工作。为什么我不能使用包含相同值的int呢?
我不需要变量大小(否则我应该使用向量),
我正在尝试创建一个名为Sink的类,它将创建一个指向传入类的指针,这是在RAII中包装一个api。
在此代码的完整版本中,自定义类也继承自另一个类,并且有静态资产来检查这一点。指针也被传递给api。
但为了简单起见,我把这个去掉了。
这是我从得到的错误
In function 'int main()':
43:30: error: no matching function for call to 'Sink<OneArg>::Sink(int)'
43:30: note: candidate is:
10:5: note: Sink<Cust
这是静态断言技巧的一部分。我不能理解非专门化的类是如何工作的。有人能给我解释一下吗?
编辑:带有宏的完整代码:(摘自)
#ifndef STATICASSERT_HXX
#define STATICASSERT_HXX
/*
Lifted direct from:
Modern C++ Design: Generic Programming and Design Patterns Applied
Section 2.1
by Andrei Alexandrescu
*/
namespace ww
{
template<bool> class compile_time