小编tom*_*tom的帖子

使用命令行从谷歌下载图像

我想下载谷歌给我的命令行的第n个图像,即与命令一样 wget

要搜索[something]我的图像,只需转到页面,https://www.google.cz/search?q=[something]&tbm=isch但如何获取第n个搜索结果的网址,以便我可以使用wget?

linux shell web

12
推荐指数
2
解决办法
2万
查看次数

索引相等的归纳类型意味着索引相等

让我们有一个归纳类型为的foo索引x : X

Parameter X : Type.

Inductive foo : X -> Type :=
| constr : forall (x : X), foo x.
Run Code Online (Sandbox Code Playgroud)

如果foo x = foo y暗示的话,我很好奇x = y。我不知道如何证明这一点。

Lemma type_equality_implies_index_equality : forall (x y : X), foo x = foo y -> x = y.
Run Code Online (Sandbox Code Playgroud)

如果无法证明,为什么?

coq

9
推荐指数
1
解决办法
54
查看次数

[[no_unique_address]] and two member values of the same type

I'm playing around with [[no_unique_address]] in c++20.

In the example on cppreference we have an empty type Empty and type Z

struct Empty {}; // empty class

struct Z {
    char c;
    [[no_unique_address]] Empty e1, e2;
};
Run Code Online (Sandbox Code Playgroud)

Apparently, the size of Z has to be at least 2 because types of e1 and e2 are the same.

However, I really want to have Z with size 1. This got me thinking, what about wrapping Empty in some wrapper …

c++ struct memory-layout object-identity c++20

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

从构造函数中调用虚函数

我正在阅读有效的C++,并且有"第9项:在构造或销毁期间从不调用虚函数".而且我想知道我的代码是否正常,即使它违反了这个规则:

using namespace std;

class A{
    public:
        A(bool doLog){
            if(doLog)
               log();
        }

        virtual void log(){
            cout << "logging A\n";
        }
};


class B: public A{
public:
    B(bool doLog) : A(false){
        if(doLog)
            log();
    }

    virtual void log(){
        cout << "logging B\n";
    }
};


int main() {
    A a(true);
    B b(true);
}
Run Code Online (Sandbox Code Playgroud)

这种方法有问题吗?当我做一些更复杂的事情时,我可能遇到麻烦吗?

它接近我,大多数答案都没有得到我在那里做的,他们只是再次解释为什么从构造函数调用虚函数可能有危险.

我想强调一下我的程序输出如下:

logging A
logging B
Run Code Online (Sandbox Code Playgroud)

因此,我在构造时记录A并在构造时记录B.这就是我想要的!但我问你是否发现任何错误(有潜在危险)与我的"黑客"克服在构造函数中调用虚函数的问题.

c++ constructor virtual-functions

6
推荐指数
2
解决办法
4016
查看次数

成员函数调用的对象右值传播

我有一个F带有foo不同实现的函数的结构,无论是否F是临时的

struct  F{
  void foo() &  { std::cout << "F::foo() &" << std::endl; }
  void foo() && { std::cout << "F::foo() &&" << std::endl; }
};
Run Code Online (Sandbox Code Playgroud)

另一个结构体在其函数调用中A具有 和 的副本。我想使用正确版本的. 因此,实现是:FbarF::fooF::foo()

struct A{

  void bar() & {
    f.foo();
  }

  void bar() && {
    std::move(f).foo();
  }

  F f;
};
Run Code Online (Sandbox Code Playgroud)

我想知道我是否真的必须提供A::bar(). 难道没有一种聪明的方法可以std::forward用来自动决定F::foo()应该使用哪个吗?


一次尝试:

struct B{

  void bar() {
    std::forward<F>(f).foo();
  }
  
  F f;
};
Run Code Online (Sandbox Code Playgroud)

但是,这不起作用。它F::foo() …

c++ rvalue rvalue-reference

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

Constexpr,模板和数组大小

我想将模板参数传递给函数调用,返回值用作数组的大小即

constexpr int myPow(int a, int b){
  int result = 1;
  for(int i=0;i<b;i++)
    result *= a;
  return result;
}

template <int N>
class testClass{
public:
  testClass(){}
  int array[myPow(2,N)];
};

int main(){
  testClass<3> A;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译器错误:

~ $ g++-4.6 test.cpp -std=gnu++0x
test.cpp: In function ‘constexpr int myPow(int, int)’:
test.cpp:6:1: error: body of constexpr function ‘constexpr int myPow(int, int)’ not a return-statement
test.cpp: At global scope:
test.cpp:12:23: error: array bound is not an integer constant before ‘]’ token
Run Code Online (Sandbox Code Playgroud)

知道怎么解决这个问题吗?

c++ templates c++11

4
推荐指数
2
解决办法
2372
查看次数

类型别名和模板模板参数推导

问题:

我注意到在使用类型别名时模板模板推导过程中存在一些不一致。特别是类型别名可以用作模板模板参数,但不能作为一个来推断。

例子:

我们将Matched<Type>用来查看是否Type是模板化类型的实例。

template <typename T>
bool Matched = false;

template <template <typename> typename F, typename T>
bool Matched<F<T>> = true;
Run Code Online (Sandbox Code Playgroud)

现在定义一个类型别名

template<typename T>
using Alias = std::tuple<T,T>;
Run Code Online (Sandbox Code Playgroud)

而我的问题是Matched<Alias<int>>==false。但是Alias可以用作模板模板参数,例如:

template<template<typename> typename F>
using ApplyInt = F<int>;
Run Code Online (Sandbox Code Playgroud)

然后ApplyInt<Alias>工作正常。

回顾一下, inApplyInt<Alias>被视为模板模板参数,而不是 in Matched<Alias<int>>。我觉得这有点愚蠢,因为我认为类型别名是类型上的函数,我想使用它们。现在,与类型相比,类型别名被视为二等公民,这使得很难以通用方式使用它们,例如组合或转换它们。

可能的出路:

1.更改推导规则,以便将类型别名检测为模板模板参数。这将使Matched<Alias>==true.

2.允许using在模板声明中使用这样的:

template<template<typename> using T, typename T>
bool Matched<F<T>> = true;
Run Code Online (Sandbox Code Playgroud)

题:

这种行为是故意的吗?这是疏忽吗?是否注意到了这一点,是否会在 C++ 的未来版本中修复?


附带说明:类似的问题是变量模板。为什么我们不能写?

template <template<typename> auto Var> …
Run Code Online (Sandbox Code Playgroud)

c++ templates typedef type-deduction

4
推荐指数
1
解决办法
491
查看次数

SFINAE用于功能体

如果函数体没有意义(即不编译),我经常使用SFINAE从重载集中删除函数.是否可以向C++添加一个简单的require声明?

例如,让我们有一个功能:

template <typename T>
T twice(T t) {
  return 2 * t;
}
Run Code Online (Sandbox Code Playgroud)

然后我得到:

twice(1.0);
twice("hello");  // Error: invalid operands of types ‘int’ and ‘const char*’ to binary ‘operator*’
Run Code Online (Sandbox Code Playgroud)

我想得到一个错误,说明没有twice类型参数的函数const char *

我想写点类似的东西:

template <typename T>
requires function_body_compiles
T twice(T t) {
  return 2 * t;
}
Run Code Online (Sandbox Code Playgroud)

然后我会得到

twice(1.0);
twice("hello");  // Error: no matching function for call to ‘twice2(const char [6])’
Run Code Online (Sandbox Code Playgroud)

更多的动力:我正在观看关于琐碎类的移动语义的梦魇和他的最终SFINAE基本上说:在编译时使用这个构造函数.对于更复杂的构造函数来说,编写正确的SFINAE将是一场噩梦.

你认为添加requires function_body_compiles到c ++会有意义吗?还是我缺少一个根本问题?这会被滥用或滥用的严重程度如何?

c++ templates c++20

3
推荐指数
1
解决办法
305
查看次数

像概念的'declval`之类的东西

当您使用模板时,decltype即使您没有任何时刻,也经常需要某种类型的实例.在这种情况下,std::declval<T>()非常有用.这会创建一个虚构的类型实例T.

概念有类似的东西吗?即一个为概念创建和想象类型的函数.

让我举个例子(有点做作,但应该达到目的):

让我们定义一个概念 Incrementable

template <typename T>
concept Incrementable = requires(T t){
   { ++t } -> T;                       
};
Run Code Online (Sandbox Code Playgroud)

现在我想有一个概念来测试一个对象是否具有operator()可以接受的运算符 Incrementable.在我想象的语法中,我会写这样的东西:

template <typename F, typename T = declval<Incrementable>>
concept OperatesOnIncrementable = requires(F f, T t){
   { f(t) } -> T;
} 
Run Code Online (Sandbox Code Playgroud)

在那里declvalin typename T = declval<Incrementable>会创建一个虚构的类型T,它实际上不是一个具体的类型,但是对于所有意图和目的来说,行为就像一个满足的类型Incrementable.

在即将出台的标准中是否有机制允许这样做?我觉得这非常有用.


编辑:前段时间我问过类似的问题是否可以这样做boost::hana.


编辑:为什么这有用?例如,如果要编写一个组成两个函数的函数

template <typename F, typename G>
auto compose(F f, G g) { …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++-concepts c++20

3
推荐指数
2
解决办法
143
查看次数

python正在窃取我的对象

好吧,我只是学习Python而且我遇到了一些奇怪的行为.我想垃圾收集器对此负责,但我不确定.

抱歉,我解读了这个问题

我想重现一些我在使用没有该库的库时得到的奇怪但我失败了.

所以第二次尝试:

我正在使用Autodesk Maya的python API.它只是围绕现有C++ API的python包装.

所以这段代码:

import maya.OpenMaya as om

Q = om.MQuaternion(1,2,3,4).conjugateIt()
P = om.MQuaternion(6,6,6,6)
print(Q[0],Q[1],Q[2],Q[3])
print(type(Q))
print(type(P))
Run Code Online (Sandbox Code Playgroud)

产生这个输出:

(6.0, 6.0, 6.0, 6.0)
<class 'maya.OpenMaya.MQuaternion'>
<class 'maya.OpenMaya.MQuaternion'>
Run Code Online (Sandbox Code Playgroud)

所以两者P,Q都是类型,MQuaternionQ不保留它应该保存的数据.在这里你可以找到文档MQuaternion类.conjugateIt是共轭的,并通过引用返回.

那么现在出了什么问题?


这是一个错误的旧问题:D

在C++中,我习惯做这样的事情.

complex<float> c = complex<float>(1,2).conjInPlace()
Run Code Online (Sandbox Code Playgroud)

conjInPlace()就是结合

但如果我在python中做类似的事情,我就会陷入困境

class testClass:
        def __init__(self,_a,_b):
            self.a = _a
            self.b = _b
        def alterMe(self):
            self.b = 123

A = testClass(1,2)
A.alterMe()
print(A.a,A.b)
B = testClass(0,0)
B = testClass(3,4).alterMe()
print(B) …
Run Code Online (Sandbox Code Playgroud)

python garbage-collection

2
推荐指数
1
解决办法
302
查看次数