在c ++ 11中,将'override'说明符添加到派生类中的虚方法中,声明了覆盖基类中的虚函数的意图.
有没有办法在C++ 03中实现类似的效果,假设它只能修改派生类而不是基类?
例如,我知道我的代码中只需要std命名空间中的"cout",所以我将它称为:
using std::cout;
Run Code Online (Sandbox Code Playgroud)
所以我可以自由地使用它:
cout << "Using namespaces like a boss!" << std::endl;
Run Code Online (Sandbox Code Playgroud)
而不是将整个命名空间带到我的代码中.
因为我被告知使用命名空间是一种不好的做法,因为它可以与其他命名空间中的其他函数交叉(幸运的是,它不是我的情况,我仍然是学习编码,我在我的职业生涯的第二个学期),但我不想将std添加到一切,我想做"使用std :: cout"和其他类似的东西,这样我可以提高代码的可读性,这对我来说在某个时间点理解我的代码很重要.
是否有用于元编程的STL功能和其他库的constexpr或其他编译时间等价物?更具体地说,我正在尝试编写一些使用SFINAE来评估某些条件并生成相应类型的元程序.例:
template<int A, int B>
enable_if_t<(A < B)> my_func() {// do something
}
template<int A, int B>
enable_if_t<!(A < B)> my_func() {// do nothing
}
Run Code Online (Sandbox Code Playgroud)
理想情况下,我希望用户能够传入比较器(如std::less<int>),而不是硬编码<.所以类似于:
template<int A, int B, class comp = std::less<int>>
enable_if_t<comp(A, B)> my_func() {// do something
}
template<int A, int B, class comp = std::less<int>>
enable_if_t<comp(A, B)> my_func() {// do nothing
}
Run Code Online (Sandbox Code Playgroud)
但是,由于函数对象不是常量表达式,因此在编译时不会对它们进行求值,因此这不起作用.实现这样的事情的正确方法是什么?
似乎某些容器接受std::pair<const int, int>为值类型,但有些容器不接受.问题当然在于该const部分.
我做了一些谷歌搜索,发现只std::vector需要可复制的数据.然而,std::pair<const int, int>工作得很好std::vector,std::set和std::list(也许是其他容器),但没有std::map和std::priority_queue(后者真的很烦我).
以下编译没有问题(gcc 6.1.0)
std::vector<std::pair<const int, int>> vector;
vector.push_back(std::make_pair(3, 5));
std::set<std::pair<const int, int>> set;
set.insert(std::make_pair(3, 5));
std::list<std::pair<const int, int>> list;
list.push_back(std::make_pair(3, 5));
Run Code Online (Sandbox Code Playgroud)
但这会导致编译错误:
std::priority_queue<std::pair<const int, int>> pq;
pq.push(std::make_pair(3, 5));
std::map<int, std::pair<const int, int>> map;
map[2] = std::make_pair(3, 5);
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)error: assignment of read-only member ‘std::pair<const int, int>::first’
这背后的原因是什么?鉴于它们具有相同的底层实现,不应该std::map并且std::set具有相同的行为吗?为什么std::vector它需要移动数据呢?
尝试使用std :: function和std :: bind绑定方法时遇到问题.
在我的CommunicationService类中:
this->httpServer->BindGET(std::bind(&CommunicationService::ManageGETRequest, this, std::placeholders::_1));
Run Code Online (Sandbox Code Playgroud)
CommunicationService :: ManageGetRequest签名:
MessageContent CommunicationService::ManageGetRequest(std::string uri, MessageContent msgContent)
Run Code Online (Sandbox Code Playgroud)
BindGET签名:
void RESTServer::BindGET(RequestFunction getMethod)
Run Code Online (Sandbox Code Playgroud)
RequestFunction typedef:
typedef std::function<MessageContent(std::string, MessageContent)> RequestFunction;
Run Code Online (Sandbox Code Playgroud)
BindGET上的错误:
错误C2664:'void RESTServer :: BindGET(RequestFunction)':无法从'std :: _ Binder <std :: _ Unforced,MessageContent(__cdecl communication :: CommunicationService ::*)(std :: string,MessageContent)转换参数1, communication :: CommunicationService*const,const std :: _ Ph <1>&>'to'RequestFunction'
之前,我的RequestFunction是这样的:
typedef std::function<void(std::string)> RequestFunction;
Run Code Online (Sandbox Code Playgroud)
它工作得很好.(当然,调整了所有签名方法).
我不明白导致错误的原因.
我正在研究一个复杂的框架,它使用std::function<>许多函数作为参数.通过剖析我发现以下一个性能问题.
有人可以解释为什么Loop3a这么慢吗?我预计将使用内联,时间也是一样的.装配也一样.有没有办法改善表现或不同的方式?C++ 17是否以这种方式做出任何改变?
#include <iostream>
#include <functional>
#include <chrono>
#include <cmath>
static const unsigned N = 300;
struct Loop3a
{
void impl()
{
sum = 0.0;
for (unsigned i = 1; i <= N; ++i) {
for (unsigned j = 1; j <= N; ++j) {
for (unsigned k = 1; k <= N; ++k) {
sum += fn(i, j, k);
}
}
}
}
std::function<double(double, double, double)> fn = [](double a, double b, double c) {
const …Run Code Online (Sandbox Code Playgroud) 我operator bool()在课外定义函数有问题
class A{
public:
explicit operator bool() const;
};
Run Code Online (Sandbox Code Playgroud)
我在课外定义函数为......
explicit A::operator bool() const {
...
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误 - error: ‘explicit’ outside class declaration
做错了什么?
#include <iostream>
#include <string>
using namespace std;
template <typename T,
typename T::type N,
typename T::strata& X>
struct SomeClass{};
struct S1
{
typedef int type;
typedef string strata;
};
int main () {
SomeClass<S1, 3, string("erg")> x;
}
Run Code Online (Sandbox Code Playgroud)
失败并显示消息:
Run Code Online (Sandbox Code Playgroud)g++ templ.cc -o templ -std=c++14 templ.cc:18:20: error: non-type template argument does not refer to any declaration SomeClass<S1, 3, string("erg")> x; ^~~~~~~~~~~~~ templ.cc:8:24: note: template parameter is declared here typename T::strata& X>
为什么它适用于 int 而不适用于字符串?为什么它说字符串是非类型参数?
我正在查看http://en.cppreference.com/w/cpp/utility/variant/visit上的文章std::variant
该示例基本上包含以下几行(由我轻轻修改):
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
auto a = overloaded {
[](auto arg) { std::cout << arg << ' '; },
[](double arg) { std::cout << std::fixed << arg << ' '; },
[](const std::string& arg) { std::cout << std::quoted(arg) << ' '; },
};
Run Code Online (Sandbox Code Playgroud)
代码基本上使用列表中的每个lambda函数作为struct的基类overloaded.第一行将lambda operator()引入结构的范围.第二行使用类模板参数推导指南(C++ 17).
题
我不明白第3行{ }后使用括号overloaded.
这里有什么样的C++机制?我们是否使用初始化列表并将其转换为可变参数模板参数,或者它是一种统一/聚合初始化?是否在这一行中调用了任何实际的构造函数?
有趣的是,如果我使用,施工将失败( ).
c++ lambda multiple-inheritance template-argument-deduction c++17
for当我弄清楚它是如何工作的时候,我正在阅读关于"范围"的陈述.
下面是一个将字符串转换为大写的程序.
string s("Hello World!!!");
//convert s to uppercase
for( auto &c :s ) // for every char in s
c= topper(c); // c is a reference,so the assignment changes the
// char in s
cout<< s << endl;
Run Code Online (Sandbox Code Playgroud)
对字符串的引用(即,c)是如何将元素更改为upppercase的?
我已经搜索了迭代如何在这里工作,但我找不到答案.