代码:
#include <memory>
struct Data;
std::unique_ptr<Data> make_me();
int main()
{
std::unique_ptr<Data> m = make_me();
return 0;
}
当然哪一个失败了:
In file included from <source>:1:
In file included from /opt/compiler-explorer/gcc-7.1.0/lib/gcc/x86_64-linux-gnu/7.1.0/../../
在c#中,静态成员对于每个泛型类都是唯一的,如下例所示
using System;
//A generic class
public class GenTest<T>
{
//A static variable - will be created for each type on refraction
static CountedInstances OnePerType = new CountedInstances();
//a data member
private T mT;
//simple constructor
public GenTest(
请考虑以下示例:
template<typename T>
class Base
{
public:
inline void fooBase ()
{
T t; // The following error only occurs when class ABC is not defined at the end of the file: "error: t uses undefined class ABC"
}
protected:
};
class ABC;
class DEF;
class Derived : public Base<
我们是否可以显式地部分实例化C++模板?
template class <typename T, int N>
class MyClass {
...
};
template<int N> class MyClass<int, N>; // not meant for specification
template<int N> class MyClass<float, N>;
就像我们可以拥有:
template class <typename T>
class MyClass {
...
};
templa
为什么此报告出现错误:
class a {
public:
void b() {this->c++;}
};
int main() {
a var;
}
但这不是?
template <typename d> class a {
public:
void b() {this->c++;}
};
int main() {
a<int> var;
}
尽管"a“是一个模板化的类,但函数"b",或者至少是对变量"c”的访问,不依赖于类型"d",因此它应该报告某些内容。
但是,如果
以下代码无法按预期编译:
#include<iostream>
class Enclosing {
int x;
class Nested { int y; };
void EnclosingFun(Nested *n) {
std::cout << n->y; // Compiler Error: y is private in Nested
}
};
但是,如果我将EnclosingFun转换为模板成员函数,编译器(gcc-7)不会抱怨访问y:
#include<iostream>
class E
在C++中,可以使用一个函数本地类型的函数:
int main() {
struct S { static void M(const S& s) { } };
S s;
S::M(s);
}
但是,如果有一个模板可以做到:
template<typename T> void Foo(const T& t) { }
int main() {
struct S { } s;
Foo(s); // Line 5: error: no matching function for call to 'Foo(main()::S&)
我正试图使用汇编程序代码-- ARMv7-a来实现C++函数。现在我遇到一个程序,我不知道如何在程序集中实现C++函数模板。我尝试用-S -O1标志编译源代码,以查看生成的程序集,但无法理解。有谁能简单地告诉我C++模板是如何被转换成组装代码的?只需使用以下简单函数作为示例:
template<typename T>
T f(T a) {
return a + 1;
}
如果您发现任何其他函数更容易进行解释,请这样做。谢谢!
我有一个模板类:
template<typename A, typename B>
class A {
};
在另一个.h文件中
template<typename A, typename B>
class A; //forward declaration
我怎么才能在这里申报m_A?我一直得到C++禁止在没有类型错误的情况下声明A:
class B {
A* m_A;
};
考虑以下例子:
#include <iostream>
#include <functional>
using namespace std;
void f(int x,int y) {}
// void f(int x) {} // if this line is uncommented, code does not compile.
int main() {
auto functor = std::bind(&f,1,placeholders::_1);
cout << "works!" << en
以下是无效代码:
struct foo {
struct bar;
bar x; // error: field x has incomplete type
struct bar{ int value{42}; };
};
int main() { return foo{}.x.value; }
这是非常清楚的,因为在定义foo::bar的时候,foo::x被认为是不完整的。
然而,似乎有一种“解决办法”使相同的类定义有效:
template <typename = void>
struct foo_impl {
struct bar
不幸的是,我主要在Java上下文中学习了类设计和设计模式;因此,有时我很难将熟悉的模式转换为C++。
假设我们希望有一个基类,它的功能由一个子类扩展。在Java中,我们会这样做:
public class BaseClass<T> {
//T is used here
protected int foo = 0;
}
public class DerivedClass<T> extends BaseClass<T> {
public void incr_foo() {
++foo;
}