是否有用于元编程的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 而不适用于字符串?为什么它说字符串是非类型参数?
标题很难用单词表达,但这是我试图在不可编译的代码中实现的:
template<template <typename> class Container>
Container<int> foo() {
return Container<int>{1,2,3};
}
int main() {
auto bar = foo<std::vector>();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
基本上我想要一个模板函数,它可以从传递给它的类型和先前已知的类型(在这种情况下int)中"组合"它的返回类型.在这种情况下,我想要一个函数,它在调用者指定的容器内返回任意数据类型.(任意地,我不是指在编译时是随机的或未确定的,而是调用者没有关于数据类型将是什么的"输入",这是在函数本身内部确定的).
这种类型的东西甚至可以通过使用std + 1z的clang或gcc来实现吗?我错过了一些非常明显的东西吗 是否存在跨越数百个角色的"1线"解决方案,我不知道?
我在这里看到了类似事物的各种例子,但它们似乎都假设函数将指针或引用作为参数并填充这些容器.
c++ templates metaprogramming template-meta-programming c++17
我想要一个非常友好的ToString功能,包括许多类型std::tuple.功能是这样的:
template <typename T>
inline std::string ToString(const T &t) {
std::stringstream ss;
ss << t;
return ss.str();
}
template <typename... Args>
inline std::string ToString(const std::tuple<Args...> &t) {
std::stringstream ss;
for (int i = 0; i < t.size(); i++) {
ss << ToString(std::get<i>(t)) << " ";
}
return ss.str();
}
Run Code Online (Sandbox Code Playgroud)
第二部分是语法错误,如何用c ++ 11模板实现它?
而且,如何实现FromString这样的:
template <typename T>
inline T FromString(const std::string &s) {
std::stringstream ss(s);
T t;
ss >> t;
return t;
}
template <typname... …Run Code Online (Sandbox Code Playgroud) 我正在查看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
如果我在 C++ 中使用累加函数,如下所示
std::vector<int> v2{1, 2, 3, 4, 5};
int sum = 0;
std::cout << std::accumulate(v2.begin(), v2.end())
Run Code Online (Sandbox Code Playgroud)
它将简单地将所有数字相加。但我想计算 1-2+3-4+5
我正在考虑一些方法来分别累积 1,3,5 和 2,4 然后做它
但不知道如何使用累积来实现这一点。
c++ ×10
c++11 ×6
templates ×4
c++17 ×3
std-function ×2
accumulate ×1
c++14 ×1
explicit ×1
lambda ×1
sfinae ×1
std ×1
stdbind ×1
stdtuple ×1
stl ×1