小编bar*_*-md的帖子

C++ constexpr计数构造函数调用

我正在尝试在编译时静态实例化一些对象.我需要的是设置成员int变量增量值.例如,我创建的第一个对象将具有0值,第二个1,第三个2 ...总结我需要这样的东西,但它必须作为constexpr工作.

//header
class MyClass final {
private:
    static int IDcount;
public:
    const int ID;
    constexpr MyClass(args);
    //~MyClass();
};
//source
int MyClass::IDcount = 0;
constexpr MyClass::MyClass(args) : ID(MyClass::IDcount++) {
}
Run Code Online (Sandbox Code Playgroud)

有没有办法在编译时实现这一点(不将ID作为构造函数的参数)

c++ instantiation constexpr

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

Java:LSParser和DocumentBuilder有什么区别

我找不到任何有关的信息org.w3c.dom.ls.LSParser.我知道这是一个界面,但有一个唯一的方法来获得一个具体的对象.

DOMImplementationLS factory = (DOMImplementationLS) myXMLDocument.getImplementation();
LSParser parser = factory.createLSParser(DOMImplementationLS.MODE_ASYNCHRONOUS, null);
Run Code Online (Sandbox Code Playgroud)

LSParser与javax.xml.parsers.DocumentBuilder(或SAXParser)有何不同

java xml parsing dom domdocument

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

C++ Constexpr模板类型成员

我想创建一个模板类,其成员是一个constexpr数组.当然,数组需要根据其类型进行不同的初始化,但我不能在不初始化的情况下声明数组.问题是我在模板专业化之前不知道数组的值.

//A.hpp
template<typename T>
class A {
public:
    static constexpr T a[];
    constexpr A() {};
    ~A() {};
}
//B.hpp
class B: public A<int> {
public:
    constexpr B();
    ~B();
};
//B.cpp
template<>
constexpr int A<int>::a[]={1,2,3,4,5};
B::B() {}
B::~B() {}
Run Code Online (Sandbox Code Playgroud)

如何正确初始化B中的A :: a []?

c++ templates initialization constexpr c++11

6
推荐指数
1
解决办法
643
查看次数

从临时对象返回左值引用

是,返回左值引用*this,允许何时*this是右值?

#include <iostream>
#include <string>
using namespace std;

class A {
public:
    A& f() {
        return *this;
    }
    string val() const {
        return "works";
    }
};

int main() {
    cout << A{}.f().val();
}
Run Code Online (Sandbox Code Playgroud)

是否有任何情况下返回的值f()将在某个时刻成为悬空参考?

f()如果这是一个像示例一样的右值,那么调用是否会延长调用者的生命周期?

c++ reference lifetime rvalue lvalue

6
推荐指数
1
解决办法
254
查看次数

类型擦除上下文中的 C++ 内存分配(使用分配器)

标准 C++ 库中有许多类可能会分配内存但不接受分配器。其中一些这样做是因为在类型擦除的上下文中分配内存是不可能的。

一个例子是 std::any 有一个构造函数,该构造函数在其设计的某个时刻接受了一个 Allocator 参数,但由于似乎无法实现而被丢弃。我确实对这种情况考虑了一段时间,我想知道阻止其实施的确切问题是什么不能满足标准的哪些要求

假设我们从any. 分配内存很简单:

struct any {

    struct type_interface;

    template <typename T>
    struct type_impl;

    type_interface* value;

    any(T&& value, const Allocator& allocator = Allocator()) {
        using actual_allocator_t
            = std::allocator_traits<Allocator>::rebind_alloc<type_impl<T>>;
        actual_allocator_t actual_allocator;
        // do allocate
        // do construct
        // assign obtained pointer
    }
};
Run Code Online (Sandbox Code Playgroud)

问题显然是我们失去了最初分配对象的分配器type_impl<T>。一个技巧可能是创建一个方法来声明一个静态变量来存储该分配器。

template <typename Allocator>
auto& get_allocator(const Allocator& allocator) {
    using actual_allocator_t = std::allocator_traits<Allocator>::rebind_alloc<type_impl<T>>;
    // static variable: initialized just on the …
Run Code Online (Sandbox Code Playgroud)

c++ memory-management type-erasure allocator c++-standard-library

6
推荐指数
0
解决办法
173
查看次数

多重派发多个参数

是否有一种优雅的方法来获取具有(单个)动态调度的OO语言中具有2个参数(或更多参数)的方法的多个调度?

可能的问题示例:

这是一个受Java启发的示例。(问题与语言无关!)

// Visitable elements
abstract class Operand {
}
class Integer extends Operand {
    int value;
    public int getValue() {
        return value;
    }
}
class Matrix extends Operand {
    int[][] value;
    public int[][] getValue() {
        return value;
    }
}

// Visitors
abstract class Operator {
    // Binary operator
    public Operand eval(Operand a, Operand b) {
        return null; // unknown operation
    }
}
class Addition extends Operator {
    public Operand eval(Integer a, Integer b) {
        return new Integer(a.getValue() …
Run Code Online (Sandbox Code Playgroud)

java oop dynamic parameter-passing dispatch

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

Kotlin编译器无法弄清楚变量在do-while循环中不可为空

我有以下方法.它的逻辑很简单,如果设置了right,那么当它有一个值(非null)时调用left.当我用以下方式编写它时,它可以工作.

fun goNext(from: Node): Node? {
    var prev : Node = from
    var next : Node? = from.right
    if (next != null) {
        prev = next
        next = next.left
        while (next != null) {
            prev = next
            next = next.left
        }
    }
    return prev
}
Run Code Online (Sandbox Code Playgroud)

相反,如果我尝试使用do-while循环来缩短代码,那么它就不再是智能强制转换nextNode.它显示了这个错误:

Type mismatch.
Required: Node<T>
Found: Node<T>?
Run Code Online (Sandbox Code Playgroud)

代码如下:

fun goNext(from: Node): Node? {
    var prev : Node = from
    var next : Node? = from.right
    if (next != null) {
        do …
Run Code Online (Sandbox Code Playgroud)

null nullable do-while kotlin

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

在使用泛型时,javac编译器是否为每种类型创建了不同的类?

如果我有一个泛型类,编译器是否为我使用它的每个类型创建了一个不同的类?让我们考虑一下Class<T>.如果我创建类型的两个实例Class<Integer>Class<String>,该编译器创建两个不同的类别?

如果答案是否定的:扩展泛型类的类如何可以继承具有不同类型(来自单个类)的相同方法或属性.

另一个问题:为什么我不能var instanceof Class<Integer>使用参数化类型而不是ClassClass<?>

如果我尝试这样做,我会收到以下错误:"无法对参数化类型执行instanceof检查Test<Integer>.请改用表单,Test<?>因为在运行时将删除更多泛型类型信息"

你能给我更多关于仿制药的信息吗?

java compiler-construction generics bytecode class

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

C++ 从左值引用转换为右值引用unique_ptr

我正在研究 C++ 中的完美转发机制,我对该std::move()功能有一些疑问。这是一个可能的实现:

template<class T> 
typename remove_reference<T>::type&&
std::move(T&& a) noexcept {
  typedef typename remove_reference<T>::type&& RvalRef;
  return static_cast<RvalRef>(a);
}
Run Code Online (Sandbox Code Playgroud)

当在 上使用时std::unique_ptr<>,此函数将资源的所有权从一个指针转移到另一个指针。我发现这个函数的焦点是从左值引用到右值引用到推导T类型的转换。

#include <iostream>
#include <memory>
using namespace std;
int main() {
    unique_ptr<int> p1(new int(20));
    unique_ptr<int> p2;
    unique_ptr<int> &r = p1;

    cout << "p1 = " << p1.get() << endl;
    cout << "p2 = " << p2.get() << endl;

    // These 2 instructions produce the same effect (then consider just one of them).
    p2 = static_cast<unique_ptr<int>&&>(r); …
Run Code Online (Sandbox Code Playgroud)

c++ casting reference perfect-forwarding c++11

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