我的应用程序编译器最多只能支持c++11。
下面是我的项目的代码,函数get_conn()返回std::unique_ptr自定义删除器(deleter需要两个参数)。我正在使用auto关键字作为返回类型,但是它给出了一个错误,例如if是否用c++11(用编译很好c++14)
error: ‘get_conn’ function uses ‘auto’ type specifier without trailing return type
Run Code Online (Sandbox Code Playgroud)
示范代码示例:
#include <iostream>
#include <functional>
#include <memory>
using namespace std;
// Dummy definition of API calls
int* open_conn (int handle)
{
return new int;
}
void close_conn (int handle, int *conn)
{}
auto get_conn (int handle)
{
// API call
int* conn = open_conn (handle);
auto delete_conn = [](int *conn, int handle) {
// API …Run Code Online (Sandbox Code Playgroud) I am 4 hours-new to C++ and have hit a brick wall with string vectors. When trying to add multiple strings to a string vector, I keep erroring out. I'd like to use push_back.
I would also like to display this string vector, but I'm not sure how (I know how to display non-vectors). Given that I have not been able to add a string to a vector of strings, I did not attempt to display the vector of …
我对如何实例化此模板感到有些困惑。我知道,简单地使用friend会员身份会实现我想要的东西会更容易,但是如果我强迫这样做,该怎么办?我只是想弄清楚。(顺便说一句,我知道这个模板似乎毫无意义),我只想使其编译即可。
#include <iostream>
template <typename T>
inline std::ostream& operator<< (std::ostream& os, const T& date)
{
os << date.getD() << " " << date.getM() << " " << date.getY() << "\n";
return os;
}
class Date
{
private:
int dd, mm, yy;
public:
Date(int d, int m, int y) : dd(d), mm(m), yy(y) {}
int getD() const;
int getM() const;
int getY() const;
};
int Date::getD() const { return dd; }
int Date::getM() const { return mm; }
int …Run Code Online (Sandbox Code Playgroud) 我写了函数。但老师告诉我,在3 个的参数的std::count_if功能,有必要通过拉姆达找出如果这封信是元音。
我不知道如何在那里转移它。
unsigned CalculateVowels(const std::string& str)
{
const std::string& vowels = "aeiouAEIOU";
unsigned count = std::count_if(str.begin(), str.end(), [](int index) {return str[index] == vowels[index]; })
return count;
}
Run Code Online (Sandbox Code Playgroud) 我有一个在本地函数中可以正常工作的代码:
struct Divider
{
public:
size_t factor;
size_t next;
};
void foo()
{
auto cmp = [](const Divider& x, const Divider& y) { return x.next > y.next; };
std::priority_queue < Divider, std::vector<Divider>, decltype(cmp)> sieve(cmp);
// ...
}
Run Code Online (Sandbox Code Playgroud)
我现在想将我的sieve变量移到一个类中。我可以写出以下怪物:
class Bar
{
inline static auto cmp = [](const Divider& x, const Divider& y) { return x.next > y.next; };
std::priority_queue < Divider, std::vector<Divider>, decltype(cmp)> sieve = std::priority_queue < Divider, std::vector<Divider>, decltype(cmp)>(cmp);
};
Run Code Online (Sandbox Code Playgroud)
有什么方法可以编写这种默认构造而无需两次指定类型?或者只是以一种更清洁的方式。
这个问题继续 非静态数据成员类推导
这是未命名的参数函数,我用它来返回std::string数据类型的表示
struct Boo {};
struct Foo {};
std::string class2str(const double) { return "Floating"; };
std::string class2str(const int) { return "Fixed Point"; };
std::string class2str(const Foo) { return "Class Foo"; };
std::string class2str(const Boo) { return "Class Boo"; };
int main(int argc, char* argv[])
{
int x_a;
double x_b;
Foo F;
Boo B;
std::cout << "x_a :" << class2str(x_a) << std::endl;
std::cout << "x_b :" << class2str(x_b) << std::endl;
std::cout << "Foo :" << class2str(F) << std::endl; …Run Code Online (Sandbox Code Playgroud) 我有两个类似的功能。这两个函数都包含一个嵌套for循环。我如何结合这两个函数来减少重复的代码。
funcA和之间的唯一区别funcB是循环中的funcB调用。func_2
这两个函数如下所示。
void funcA()
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
func_1();
}
}
}
void funcB()
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
func_1();
func_2();
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在编写这样的模板函数:
template<typename T>
std::string EncodeData(int DataType, T Data, std::string ReadCommandID, std::string& ThisID);
Run Code Online (Sandbox Code Playgroud)
我想使用的类型T是一些不同的结构,例如:
struct A
{
int A_data;
bool A_data_2;
// etc.......
};
struct B
{
double B_data;
char B_data_2;
// etc.......
};
Run Code Online (Sandbox Code Playgroud)
我希望函数可以根据不同的struct传递来访问不同的成员变量T,所以我写了这样的代码:
template<typename T>
std::string EncodeData(int DataType, T Data, std::string ReadCommandID, std::string& ThisID)
{
if(std::is_same<T, A>{})
{
// Do something with Data.A_data and Data.A_data_2
}
else if(std::is_same<T, B>{})
{
// Do something with Data.B_data and Data.B_data_2
}
// other code.
}
Run Code Online (Sandbox Code Playgroud)
并使用它: …
我正在考虑编写一个既适用于序列容器又适用于关联容器的函数。这就像
template<class C, class V = typename C::key_type>
bool has_val(const C& c, const V& v)`
Run Code Online (Sandbox Code Playgroud)
在函数内部,我想
const_iterator find(key_type) const。std::find()像 之类的序列容器std::vector。检查的最佳做法是什么(1)?
如果我上面描述的不是最好的,请告知是否有更好的方法?
(不幸的是,我无法使用宏访问较新的 Folly FOLLY_create_member_invoker,但我确实有FOLLY_CREATE_HAS_MEMBER_FN_TRAITS。不过,我无法成功完成它)