对于c++模板中的空尖括号c++,我只知道两种用例,这些用例是:
( 1)在模板完全专业化的过程中。
template<typename T>
class X
{};
template<>
class X<char>
{};
2)在模板函数调用过程中,可以推导出参数类型。
template<typename T>
T add(T a, T b);
...
add<>(1, 2);
但是这个例子对我来说是全新的,我无法理解这种语法的含义(请看一下rapidxml::xml_node<>*):
class TestW
C++标准指出,非依赖基类的非限定名优于模板参数。这背后的原因是什么?
以下片段来自C++模板:
#include <iostream>
template <typename X>
class Base {
public:
int basefield;
using T = int;
};
class D1 : public Base<Base<void>> {
public:
void f() { basefield = 3; // works as normal
我有一个简单的模板结构,将一个字符串与一个值关联起来
template<typename T> struct Field
{
std::string name; T self;
}
我有一个函数,我想接受任意类型的1个或多个字段,这些字段可能是不同类型的,所以我使用std::initializer_list,因为据我所知,C++缺少类型化的可变参数,不能确定可变参数的大小,并且必须至少有一个其他参数来确定从哪里开始。
问题是我不知道如何告诉它接受可能是不同类型的字段。在Java语言中,我只使用foo(Field<?> bar, Field<?>...
首先,请随意为这个问题建议更好的标题。
考虑下列方案:
#include <numeric>
namespace N { class C {}; }
int operator+( int i, N::C ) { return i+1; }
int main() {
N::C a[10];
std::accumulate( a, a+10, 0 );
}
g++ 5.4.0:成功编译(参见实时演示 )
clang++ 3.8.0 (参见实时演示 )
错误:
In file included from source_file.cpp:3:
/usr/include
我的新C++ unordered_map有些问题:我想使用带有const密钥的operator[],但被拒绝了。
我不能给出完整的代码,但我可以像这样简化我的问题:
#include <unordered_map>
class A {
public:
A();
};
class B {
public:
B();
};
int main(int argc, char **argv) {
std::unordered_map<A &, B> myMap;
A a;
const A &
我有一个实现类似CRTP模式的模板类的层次结构。我不明白为什么名称查找在标记为错误的行中失败,而在标记为“这里没有错误”的行中成功。
class CPublishedTypes
{
public:
typedef int published_t;
};
template<class Derived, class PublishedTypes> class Cbase: public PublishedTypes
{
public:
Cbase():ibase_(42){}
private:
published_t i
我有一些在没有-fpermissive选项的情况下不再编译的C++代码。这是我不能分享的适当的代码,但我认为我已经能够提取一个简单的测试用例来演示这个问题。以下是g++的输出
template_eg.cpp: In instantiation of 'void Special_List<T>::do_other_stuff(T*) [with T = int]':
template_eg.cpp:27:35: required from here
template_eg.cpp:18:25: error: 'next' was not decla
通常,当A从B继承时,A的所有成员对B的函数都是自动可见的,例如
class A {
protected:
int a;
};
class B : public A {
int getA() {return a;}
//no need to use A::a, it is automatically visible
};
然而,当我继承模板时,这段代码就成为非法的(至少在gcc中是这样)。
template<typename T>
class A {
protected:
int a;
};
template<typename T>
当迭代器被引入到ISO C++时,我正在寻找一个引用,在这个例子中我可以注意到它们是自C++98以来与向量一起使用的,但是我从页面上读到,这不是正式文档,而是一个引用:
// constructing vectors
#include <iostream>
#include <vector>
int main ()
{
// constructors used in the same order as described above:
std::vector<int> first; //