标签: variant

VarIsEmpty和VarIsEmptyParam函数之间有什么区别

刚才在Delphi7工作,我注意到不仅VarIsEmpty存在一个函数,而且还存在一个函数VarIsEmptyParam.

由于Delphi的帮助没有给出太多解释:

如果给定的变量表示未分配的可选参数,则VarIsEmptyParam返回true.

如果变量包含任何其他值,则函数结果为false.

我只是想知道是否有人使用过这个功能,如果有的话,这个功能是如何使用的.

delphi delphi-7 variant

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

C的Variant数据类型库

是否有一个像样的开源C库来存储和操作
动态类型的变量(又名变种)?我主要对原子值(int8,int16,int32,uint,字符串,blob等)感兴趣,而JSON样式的数组和对象以及自定义对象也很好.这种库有用的一个主要情况是使用SQL数据库.

这种库最明显的特征是所有支持值的单一类型,例如:

struct Variant {
    enum Type type;
    union {
        int8_t int8_;
        int16_t int16_;
        // ...
    };
};
Run Code Online (Sandbox Code Playgroud)

其他功能可能包括将Variant对象转换为C结构(使用绑定表),将值转换为/从字符串转换,以及与现有数据库库(如SQLite)集成.

注意:我不相信这个问题是C中通用数据类型任何库的重复吗?,指的是"队列,树木,地图,名单".我所谈论的内容更多地侧重于使用SQL数据库与在解释语言中使用它们一样顺畅.

c variant c-libraries

7
推荐指数
3
解决办法
5738
查看次数

C++ - 提升问题

是否有人知道,如果boost::getboost::variant是一个性能消耗操作不.

现在我在性能关键部分重构一些旧代码,其中"变种"是通过容器为每种可能的类型和相应的实现enum.

显然,这很快,但很丑,现在当我必须重构代码以便它可以使用另一种类型时,我想摆脱旧的代码部分并替换它boost::variant.

此外,我不能简单地"描述两种变体并进行比较",因为这种重构是一种痛苦,而且非常耗时.

因此,如果有人知道boost::get<x>与泛型enum-based类型调度相比如何执行,我将非常感谢您分享这些知识.

还有另一种boost::variant<types>与自定义访问者一起使用的变体(如boost::variant文档中所述) - 这可能比boost::get我的情况更快吗?

谢谢.

c++ performance boost variant

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

为什么我得到"类型参数必须是无效的......"错误?

我将尝试缩短此代码示例:

public interface IThing
{
    //...  Stuff
}

public class Thing1 : IThing
{  
}

public class Thing2 : IThing
{  
}

public interface IThingView<out T>
{
    ICollection<T> ViewAll();
}

public class ThingView<T> : IThingView<T>
{
    ICollection<T> ViewAll() { return new List<T>(); }  //  There's a big operation here
}

public interface IThingViewerFactory
{
    public IThingView<IThing> Build(string Which);
}

public class ThingViewerFactory
{
    public IThingView<IThing> Build(string Which)
    {
        if(Which.Equals("Thing1") { return new (IThingView<IThing>)new ThingViewer<Thing1>();}
        else { return new (IThingView<IThing>)new ThingViewer<Thing2>();} …
Run Code Online (Sandbox Code Playgroud)

c# generics variant factory-pattern

7
推荐指数
2
解决办法
3418
查看次数

VB6:禁用变体

我有一个大的VB6项目,其中很多变量没有明确定义的类型,因此它们自动默认为Variant类型.手动找到所有这些是一项艰巨的任务,那么有什么方法可以实现自动化吗?在VB.Net中,可以使用"Option Strict"禁用所有变体的自动使用,但VB6没有该选项.

现在我添加DefByte A-Z到每个类,它使默认类型'Byte'而不是'Variant'.这让我在运行时捕获了很多未定义的变量,只要它们被分配了一个大于255的值.但它仍然不是完全万无一失的.

是否有更可靠的方法来检测所有未定义的变量?

compiler-construction vb6 variables strict variant

7
推荐指数
2
解决办法
612
查看次数

可以在FreePascal中的变体记录的案例中复制标识符吗?

这是我的问题:我想创建一个记录类型,其中在变体记录的情况下,一些(但不是全部)将具有某个字段.根据维基,这是完全合法的.然而,当我尝试编译以下代码时:

program example;

{$mode objfpc}{$H+}

uses sysutils;

type
  maritalStates = (single, married, widowed, divorced);

  TPerson = record
    name: record
      first, middle, last: string;
    end;
    sex: (male, female);
    dob: TDateTime;
    case maritalStatus: maritalStates of
      single: ( );
      married, widowed: (marriageDate: TDateTime);
      divorced: (marriageDate, divorceDate: TDateTime;
        isFirstDivorce: boolean)      
  end;

var
  ExPerson: TPerson;

begin
ExPerson.name.first := 'John';
ExPerson.name.middle := 'Bob';
ExPerson.name.last := 'Smith';
ExPerson.sex := male;
ExPerson.dob := StrToDate('05/05/1990');
ExPerson.maritalStatus := married;
ExPerson.marriageDate := StrToDate('04/01/2015');

end.
Run Code Online (Sandbox Code Playgroud)

编译失败,出现以下错误:

$ fpc ex.pas
Free …
Run Code Online (Sandbox Code Playgroud)

pascal freepascal record variant

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

如何流式传输std :: variant <...,...>

我的std::variant包含可流式类型:

std::variant<int, std::string> a, b;
a = 1;
b = "hi";
std::cout << a << b << std::endl;
Run Code Online (Sandbox Code Playgroud)

使用带有-std = c ++ 1z的g ++ 7进行编译会返回编译时错误.

摘录:

test.cpp: In function 'int main(int, char**)':
test.cpp:10:13: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'std::variant<int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >')
   std::cout << a << b << std::endl;
   ~~~~~~~~~~^~~~
Run Code Online (Sandbox Code Playgroud)

貌似std::variant<int, std::string>是不能流.我怎样才能实现我可以直接将变量流式传输到输出流?

预期产量:

1hi
Run Code Online (Sandbox Code Playgroud)

c++ stream variant c++17

7
推荐指数
3
解决办法
1587
查看次数

C++ 变体访问重载函数

我想在变体上执行重载函数。以下代码块可以工作并编译,但visit调用似乎过于复杂。为什么我不能简单地写:

std::visit(&f, something);
Run Code Online (Sandbox Code Playgroud)

工作版本和上下文:

#include <variant>
#include <string>
#include <iostream>
#include <functional>

struct A {
        std::string name = "spencer";
};

struct B {
        std::string type = "person";
};

struct C {
        double age = 5;
};

void f(A a) {
        std::cout << a.name << std::endl;
}

void f(B b) {
        std::cout << b.type << std::endl;
}

void f(C c) {
        std::cout << c.age << std::endl;
}


int main() {
        std::variant<A, B, C> something{B{}};
        std::visit([](auto&& x) {f(x);}, something);
} …
Run Code Online (Sandbox Code Playgroud)

c++ variant

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

C++:std::visit 不能在 gcc 下编译

这是我的代码,它对 clang 编译得很好,但在 gcc 时失败了

#include <iostream>
#include <string>
#include <regex>
#include <variant>

struct Id {
    void SetValue(const std::string& item)
    {
        value = item;
    }

    std::string value;
};

struct Number {
    void SetValue(const std::string& item)
    {
        value = std::stoi(item);
    }
    int value;
};


using TokenBase
    = std::variant<Number, Id>;

struct Token : TokenBase {
    using TokenBase::TokenBase;

    template <typename T>
    [[nodiscard]] bool Is() const {
        return std::holds_alternative<T>(*this);
    }

    template <typename T>
    [[nodiscard]] const T& As() const {
        return std::get<T>(*this);
    }

    template <typename …
Run Code Online (Sandbox Code Playgroud)

c++ gcc std variant

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

通过With语句释放变体类型对象数组元素

如果一个对象数组被声明为Variant类型(以便轻松检查它是否使用IsEmpty函数初始化),那么,如果随后定义的数组的元素被引用为With语句的对象表达式(例如With VariantObjArray(i) ...),那么对象变量数组元素将被错误地释放(尽管With语句的对象变量的隐式副本将在单个后续执行过程中正确运行 - 通过With语句的范围)。此外,数组元素对象变量的错误释放可能是内存泄漏,因为它是在执行 With 表达式时立即发生的,而不是任何标准释放机制(例如退出With语句或从子例程返回)的结果或明确设置为 Nothing。

Sub DemoVariantObjArrayBug()
    Dim i As Integer
    Dim NextWkSh As Worksheet
    Static VariantObjArray As Variant

    If IsEmpty(VariantObjArray) Then 'Check to avoid unnecessary re-allocation of static or global array variable
        ReDim VariantObjArray(1 To ThisWorkbook.Worksheets.Count)
        For Each NextWkSh In ThisWorkbook.Worksheets
            i = i + 1: Set VariantObjArray(i) = ThisWorkbook.Worksheets(i)
        Next NextWkSh
    End If
    
Stop 'and, to observe the bug, open the Locals …
Run Code Online (Sandbox Code Playgroud)

arrays vba object variant

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