小编R S*_*ahu的帖子

数据成员'vec'不能是成员模板

我在标题中有以下两行,以声明包含模板的向量:

template <class t>
    std::vector <t> vec;
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:数据成员'vec'不能是成员模板我做错了什么?编辑:我不知道我被理解正确,我试图声明一个包含模板的向量,我知道这可以完成,因为可以有以下内容:

template <class T>
void funct(vector <T> v){


}
Run Code Online (Sandbox Code Playgroud)

这个函数采用模板的向量作为它的参数,我希望做同样的事情,除了在标题中声明向量以允许向量包含任何东西.

c++ templates vector

9
推荐指数
2
解决办法
6668
查看次数

为什么std :: result_of <int(int)> :: type有效?

我已阅读以下相关问题:

  1. std :: result_of简单函数
  2. decltype,result_of或typeof?

cppreference.com 上的页面std::result_of.

所有这些似乎表明我应该能够使用:

 std::result_of<int(int)>::type v1 = 10;
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用g ++ 4.9.2构建以下程序时

#include <type_traits>

int foo()
{
   return 0;
}

int main()
{
    std::result_of<int(int)>::type v1 = 10;           // LINE A
    std::result_of<decltype(foo)>::type v2 = 20;      // LINE B
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我收到"LINE A"和"LINE B"的错误消息.错误消息是:

socc.cc: In function ‘int main()’:
socc.cc:10:5: error: ‘type’ is not a member of ‘std::result_of<int(int)>’
     std::result_of<int(int)>::type v1 = 10;
     ^
socc.cc:11:5: error: ‘type’ is not a member of ‘std::result_of<int()>’
     std::result_of<decltype(foo)>::type v2 = …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

8
推荐指数
2
解决办法
1274
查看次数

从参数包中获取typedef

我有这种形式的结构:

template <typename T>
struct X
{
    using value = T;
};
Run Code Online (Sandbox Code Playgroud)

我将其中一个或多个传递给模板函数,如下所示:

template <typename... Ts>
void Func(Ts... ts);
Run Code Online (Sandbox Code Playgroud)

我真正想要的是明确列出Xs并使函数参数类型为values Ts.我该怎么办?

c++ templates c++11

8
推荐指数
1
解决办法
247
查看次数

为什么std :: is_const <const int&> :: value计算为false?

这是如何检查对象是否为常量的问题.

我很惊讶地看到以下程序

#include <iostream>
#include <type_traits>

int main() 
{
   std::cout << std::boolalpha;
   std::cout << std::is_const<const int&>::value << "\n";
}
Run Code Online (Sandbox Code Playgroud)

产生了这个输出

false

在什么情况下将其const int&视为非const类型是有意义的?

c++ type-traits c++11

7
推荐指数
1
解决办法
501
查看次数

在vector <vector <T >>的初始化中合并vector和initializer_list?

说我初始化vector<vector<string>>就像这样:

vector<vector<string>> v;
v = {{
    {"a", "b", "c"},
    {"aa", "bb"},
    {"xyz", "yzx", "zxy"},
    {}
}};
Run Code Online (Sandbox Code Playgroud)

现在假设我想将已经存在的东西附加vector<string>到某些v元素中.像这样:

vector<string> suffix {{"1", "2", "3"}};
vector<vector<string>> v;
v = {{
    {"a", "b", "c"} + suffix,
    {"aa", "bb"},
    {"xyz", "yzx", "zxy"} + suffix,
    {}
}};
Run Code Online (Sandbox Code Playgroud)

该语法显然不起作用,因为operator+没有以这种方式定义.

我知道可以构建v第一种方式然后编写

vector<int> indices = {0, 2};
for(int i: indices)
   v[i].insert(v[i].end(), suffix.begin(), suffix.end());
Run Code Online (Sandbox Code Playgroud)

但这不方便,因为我可能有几个suffix附加到任意的向量v[i].我希望它suffix与初始化一起,v[i]所以它是有道理的,如果我从v初始化添加/删除元素,我不必转移索引.

c++ vector initializer-list c++11

7
推荐指数
1
解决办法
932
查看次数

模板专业化和实例化

这些概念对我来说有点不清楚.好吧,模板实例化的定义非常好N4296::14.7 [temp.spec]:

实例化函数,类,类模板或成员模板的成员的行为称为模板实例化.

那就是如果我们有一个函数/变量/类模板,模板的实例化只是创建一个对象或函数.例如:

template<typename T> class U{ };
U<int> a; //instantiation
Run Code Online (Sandbox Code Playgroud)

但是N4296:14.7.1 [temp.inst]说(强调我的):

除非已经显式 实例化了类模板特(14.7.2)或显式专用(14.7.3),否则在需要完全定义的对象类型或完整性的上下文中引用特化时,将隐式实例化类模板特化.类类型会影响程序的语义.

模板特化的实例化定义是什么,而不仅仅是模板的实例化?

c++ templates

7
推荐指数
2
解决办法
1842
查看次数

类模板的成员函数模板的显式实例化

假设我的头文件中有一个带有成员函数模板的类模板。

//file.hxx
template<class T>
struct A {
    T val;
    template<class U> foo(U a);
};
Run Code Online (Sandbox Code Playgroud)

我在 .cpp 中有 foo 的实现:

//file.cpp
#include "file.hxx"
#include <typeinfo>
template<class T> template<class U>
A<T>::foo<U>(U a){
    std::cout << "Type T: " << typeid(val).name() << std::endl;
    std::cout << "Type U: " << typeid(a).name() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

如果我想在 .cpp 文件中显式实例化我的类和成员函数(例如 int 和 float),我需要类似以下内容:

template struct A<int>;
template struct A<float>;
template A<int>::foo<int>(int);
template A<int>::foo<float>(float);
template A<float>::foo<int>(int);
template A<float>::foo<float>(float);
Run Code Online (Sandbox Code Playgroud)

如果我开始有很多类型的 T 和 U,这会变得有点冗长。有没有更快的方法可以做到这一点,并且仍然在 cpp 文件中显式实例化模板?我在想象类似的事情

template<class T>
template A<T>::foo<int>(int);
template<class …
Run Code Online (Sandbox Code Playgroud)

c++ templates explicit-instantiation

7
推荐指数
0
解决办法
3014
查看次数

动态风在现实世界编程中有多大用处?

我已经阅读了一些并试图理解,dynamic-wind但我不太确定如何在现实世界的应用程序中使用它。

任何有关该主题的见解都将非常有帮助。

scheme

6
推荐指数
1
解决办法
266
查看次数

如何使用 open() 和 printf() 写入文件?

我正在使用 open() 打开一个文件,并且需要使用 printf 打印到该文件,而不输出到控制台。我该怎么做呢?我可以成功创建文件,并 printf 到控制台,但这是不正确的。

int main(int argc, char *argv[]) {
    int fd;
    char *name = "helloworld";
    fd = open(name, O_CREAT);

    char *hi = "Hello World";
    printf("%s\n", hi);

    close(fd);
    exit(0);
}
Run Code Online (Sandbox Code Playgroud)

我需要程序没有输出到控制台,但是如果我查看该文件helloworld,它应该在里面写有“Hello World”。例如:

prompt> ./hello
prompt> more helloworld
   Hello World
Run Code Online (Sandbox Code Playgroud)

c file-io printf output

6
推荐指数
1
解决办法
5万
查看次数

是否在?:expression中创建的C++临时对象的生命周期是通过将其绑定到本地const引用来扩展的?

我不清楚临时对象的生命周期是通过将它绑定到?:表达式中的const引用来扩展的:

class Foo {...};

Foo *someLValue = ...;

const Foo& = someLValue ? *someLValue : Foo();
Run Code Online (Sandbox Code Playgroud)

通过将默认构造函数Foo()通过绑定到本地const ref来扩展创建临时文件的生命周期,即使绑定是有条件的吗?或者这会创建一个悬空引用,因为Foo()的临时值将在?:表达式的末尾被销毁?

c++ reference const-reference temporary-objects

6
推荐指数
1
解决办法
231
查看次数