我的 linter 声称以下代码中的 lambda 隐式捕获this(并希望显式指定该捕获)。那是对的吗?
template <class X>
struct OtherTemplate
{
static X bar()
{
return 1;
}
};
template <class Y>
struct Test
{
void foo()
{
auto lambda = [&]() {
// return (Y)1;
// return std::min<Y>(1, 2);
return OtherTemplate<Y>::bar();
};
(void)lambda;
}
};
// Explicit instantiation
template struct Test<int>;
Run Code Online (Sandbox Code Playgroud)
如果 lambda 没有默认捕获,或者使用任何注释行,则不会有任何抱怨。删除“模板化”OtherTemplate也不会产生任何诊断。
对我来说这似乎很明显this在 lambda 中没有使用。但是,以下两个问题似乎观察到了类似的行为:
C++11 标准(选择一个)向我表明捕获this需要使用 ODR this …
在以下代码段中,时间消耗为std::sort。这应该花费O(nlog(n))时间。std::chrono仅用于测量std::sort。
我使用具有优化级别的Intel编译器18.0.3编译了以下代码-O3。我使用Redhat6。
#include <vector>
#include <random>
#include <limits>
#include <iostream>
#include <chrono>
#include <algorithm>
int main() {
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> dist(std::numeric_limits<int>::min(),
std::numeric_limits<int>::max());
int ret = 0;
const unsigned int max = std::numeric_limits<unsigned int>::max();
for (auto j = 1u; j < max; j *= 10) {
std::vector<int> vec;
vec.reserve(j);
for (int i = 0; i < j; ++i) {
vec.push_back(dist(rng));
}
auto t_start = std::chrono::system_clock::now();
std::sort(vec.begin(), vec.end());
const auto …Run Code Online (Sandbox Code Playgroud) 我试图在Java Web应用程序中获取项目上下文路径.我的项目位于D:\GED\WSysGED目录中,我希望获得该路径.在我的研究中,我发现了两种方法:首先使用System.getProperty如下:
String path= System.getProperty("user.dir");
System.out.println(path);
Run Code Online (Sandbox Code Playgroud)
但是该代码返回D:\eclipse-jee-luna-R-win32\eclipse,Eclipse可执行文件所在的位置.
第二种方法是使用servlet.
我在本教程后创建了一个
public class ContextPathServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
ServletContext servletContext = getServletContext();
String contextPath = servletContext.getRealPath("/");
PrintWriter out = response.getWriter();
out.println("<br/>File system context path (in TestServlet): " + contextPath);
}
}
Run Code Online (Sandbox Code Playgroud)
但它正在显示 C:\Users\Juan\SysGED\.metadata\.plugins\org.eclipse.wst.server.core\tmp6\wtpwebapps\WSysGED
获得项目路径的正确方法是什么?
我正在研究自定义流类的可能设计。在场内和场外搜索了半个小时都没有得到我的问题的答案,所以我提出了一个新问题。
以std::endl作为一个例子。如果我没有记错的话,它被指定为大致像这样工作(认为这是“幻灯片代码”):
struct ostream
{
using FManip = ostream& (*)(ostream&);
ostream& operator<<(FManip f)
{
return f(*this);
}
};
ostream& endl(ostream& s)
{
/* ...do whatever... */
return s;
}
Run Code Online (Sandbox Code Playgroud)
https://godbolt.org/z/6MP5cseas
但是,我不清楚这样做的好处是什么,而不是拥有标记对象并operator<<直接为它们实现:
struct ostream
{
};
// (reserved name - use whatever convention fits when writing library code)
struct _EndlManipTag {};
static constexpr _EndlManipTag endl;
ostream& operator<<(ostream& s, _EndlManipTag tag)
{
/* ...do whatever... */
return s;
}
int main()
{
ostream os;
os << endl;
} …Run Code Online (Sandbox Code Playgroud) 我想在C++中编写一个将lambda函数保存为成员变量的类.尽可能高效地做它会很棒.例如,我读了这个线程为什么编译器可以比普通函数更好地优化lambdas?因此我想避免使用函数指针.
到目前为止,我最好的解决方案如下:
template<typename F>
class LambdaClass {
private:
F lambdaFunc;
public:
LambdaClass(F &_lambdaFunc): lambdaFunc(_lambdaFunc) {}
};
Run Code Online (Sandbox Code Playgroud)
我会用这个类如下:
auto lambdaFunc = [](int _a) -> int { return _a; };
LambdaClass<decltype(lambdaFunc)> lambdaClassObject<decltype(lambdaFunc)>(lambdaFunc);
Run Code Online (Sandbox Code Playgroud)
在我看来,使用它看起来并不好玩.所以我首先感兴趣的是,如果编译器可以内联已保存成员lambda函数的调用,那么这个代码是否有效,以及如何编写这个代码更漂亮?
编辑:我正在使用C++ 11.
我不得不问这个问题,这有点愚蠢,但我只是找不到一种毫不费力的方法来做到这一点。
我有以下循环:
for (int i = 0; i < count; ++i) {
if (myFunc(i))
continue;
myOtherFunc(i);
}
Run Code Online (Sandbox Code Playgroud)
与OpenMP并行实现这一点很简单:只需#pragma omp parallel for在循环之前添加即可。
我想将OMP(及其不同的计划)的性能与MSVC的并行<algorithms>实现(即使用C ++ 17执行策略)进行比较。最直接的想法是使用std::for_each,但是我想不出一种好方法将这个超简单for循环转换成可以在其上施加<algorithm>执行策略的任何适当的东西。
值得注意的是,你不能只是做
std::for_each(std::execution::par, 0, count, [](int i){ /*...*/ });
Run Code Online (Sandbox Code Playgroud)
因为您必须提供迭代器(即i在取消引用时会产生参数的东西)。
我可以std::iota进入std::vector,int因此我要遍历一系列索引。但是那将是荒谬的。
我可以使用std::generate_n一些虚拟输出迭代器,该迭代器丢弃分配的所有内容。由于我认为没有可用,因此std我必须自己编写完整的虚拟迭代器。无论如何,这当然是愚蠢的。拥有正确的索引可能需要使用a进行手动跟踪,std::atomic<int>因为您不了解当前的索引。
我真的没有容器可以循环。我的意思是,在这些函数的深处都有容器,但是重组所有内容只是为了让我可以在此循环中对某些容器使用迭代器,这是不可能的。
搜寻了15分钟的不同描述后,我却一无所获。
有什么方法可以将最简单和最基本的for循环与<algorithm>不涉及愚蠢的废话的工具相匹配?
我正在研究并行编程并在排序算法上对其进行测试。我发现最简单的方法是使用 OpenMP,因为它提供了一种实现线程的简单方法。我做了研究,发现其他人已经这样做了,然后我尝试了一些代码。但是,当我perf stat -r 10 -d在 Linux 上测试它时,我得到的时间比序列化代码更糟糕(在某些情况下是两倍)。我尝试在数组上使用不同数量的元素,我使用的最大数量是 1.000.000 个数字,就好像我使用更多我收到错误一样。
void merge(int aux[], int left, int middle, int right){
int temp[middle-left+1], temp2[right-middle];
for(int i=0; i<(middle-left+1); i++){
temp[i]=aux[left+i];
}
for(int i=0; i<(right-middle); i++){
temp2[i]=aux[middle+1+i];
}
int i=0, j=0, k=left;
while(i<(middle-left+1) && j<(right-middle))
{
if(temp[i]<temp2[j]){
aux[k++]=temp[i++];
}
else{
aux[k++]=temp2[j++];
}
}
while(i<(middle-left+1)){
aux[k++]=temp[i++];
}
while(j<(right-middle)){
aux[k++]=temp2[j++];
}
}
void mergeSort (int aux[], int left, int right){
if (left < right){
int middle = (left + right)/2;
omp_set_num_threads(2);
#pragma omp parallel …Run Code Online (Sandbox Code Playgroud) 简而言之:以下代码是否有未定义的行为,或者这样可以吗?
struct X
{
X(int b)
: value(b)
, ref(value)
{
}
int value;
int& ref;
void isThisUB() const
{
ref = 1;
}
};
int main()
{
const X x(2);
// Is either of these fine?
x.isThisUB();
x.ref = 3;
return x.value;
}
Run Code Online (Sandbox Code Playgroud)
https://godbolt.org/z/1TE9a7M4a
X::value是常量x。根据我对const语义的理解,这意味着以任何方式修改它都是UB。然而,我们可以在构造函数中获取对它的非常量引用,然后通过它来修改它,无论是在const成员函数中还是直接修改。
C++(至少 17)标准在[dcl.type.cv]中给出了与 const 相关的 UB 示例,它看起来基本相同,只是它使用const_cast. 注意如何p->x.j = 99表示为 UB。我没有看到使用const_cast上面的代码实现这一点有根本的区别。
那么,上面的代码是UB吗?非常量引用成员/指针真的有这么大吗?
(如果你能想出产生相关问题的搜索关键字,而不仅仅是随机的const东西,我会印象深刻。)
我有一个关于这个问题的后续问题:符号P并A参考temp.deduct.call部分
如果我正确理解模板参数推导,则以下代码会发生以下情况:
template<typename T>
void foo(const T& a);
int b;
foo(std::move(b));
Run Code Online (Sandbox Code Playgroud)
P和。我们正在推论声明是引用(但不是转发引用)A的情况const T&A:std::move(b)类型为int&&[xvalue] -> 调整为A:= int( [7.2.2#1] )P:const T&-> 删除 const 和引用 ([ 12.9.2.1#3 ]) ->P:= TA- P > 结果T:= int。两个问题:
std::move(b)是一个表达式,我一直认为它的类型是int&&(因为std::move返回 a int&&),但是 ( [7.2.2#1] ) 告诉了一些不同的事情,这意味着在进行任何分析之前删除每个引用,所以当人们谈论表达式的类型时,从来没有涉及任何参考:struct A{ …Run Code Online (Sandbox Code Playgroud) In my understanding, member functions defined inside a class definition are implicitly inline:
class X {
int f() { return 1; } // Implicitly inline.
};
int g() { return 2; } // Not implicitly inline.
Run Code Online (Sandbox Code Playgroud)
I was looking for the standard quote to support this, but I can only find basic.link/7:
In addition, a member function, static data member, a named class or enumeration of class scope, or an unnamed class or enumeration defined in a class-scope …