在下面的代码中,extra找不到 ,除非将泛型类型T(that extends 'complex')替换为'complex'。
export type ChoiceType = { kind: 'simple' } |
{ kind: 'complex'; extra: number };
function Func1<T extends 'complex'>(arg: Extract<ChoiceType, { kind: T }>) {
const u = arg.extra; // Error: property 'extra' does not exist...
}
function Func2(arg: Extract<ChoiceType, { kind: 'complex'}>) {
const u = arg.extra; // No error
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么 Typescriptextra在第一种情况下看不到。也许有人可以帮忙?
有没有人知道如何创建一个 C++ 概念T,以便如果存在接受参数的in的重载,则该函数g仅针对t具有类型的参数定义?TfBt
struct A1 {};
struct A2 {};
struct B {
void f(A1 a1) {}
};
void g(T t) {
B b;
b.f(t);
}
Run Code Online (Sandbox Code Playgroud)
举个例子,我想to_string为所有std::stringstream接受的东西定义一个,并定义类似的东西
std::string to_string(T t) {
std::stringstream ret;
ret << t;
return ret.str();
}
Run Code Online (Sandbox Code Playgroud)
所有关于概念的例子都处理更简单的情况,即要求一个类型上存在一个函数,而在这种情况下,我们想要检查另一个类型上一个函数的存在。
我想导入这样的函数:
[return: MarshalAs(UnmanagedType.LPWStr)]
[DllImport("DLL.dll", EntryPoint="FuncUtf16", ExactSpelling=true, PreserveSig=true, CharSet=CharSet.Unicode)]
public static extern string Func();
Run Code Online (Sandbox Code Playgroud)
但这给了我一个这样的错误:
"Windows在Test.exe中触发了断点.这可能是由于堆的损坏,这表明Test.exe或其加载的任何DLL中存在错误."
当我反复按"继续"时,该功能确实给出了预期的输出.但是,当我有机会上述声明时:
[DllImport("DLL.dll", EntryPoint="FuncUtf16", ExactSpelling=true, PreserveSig=true, CharSet=CharSet.Unicode)]
public static extern IntPtr Func();
Run Code Online (Sandbox Code Playgroud)
(将返回类型更改为IntPtr)并按如下方式调用它:
Dim a As IntPtr = Func()
Dim Str As String = Runtime.InteropServices.Marshal.PtrToStringUni(a)
Run Code Online (Sandbox Code Playgroud)
,我没有错误,它工作得很好!使用"MarshalAs"方式在dll中声明函数有什么问题?
以下按预期工作:
#include <array>
constexpr std::array<int, 3> values = {1, 2, 3};
template <int i> struct A { static constexpr int val = values[i]; };
int main() { A<1> a; }
Run Code Online (Sandbox Code Playgroud)
但是,如果我们使用values.size()模板参数,我会从MSVC编译器中收到编译器错误:
int main() { A<values.size()> a; }
Run Code Online (Sandbox Code Playgroud)
错误是表达式没有评估为常量.GCC编译没有错误.
c++ ×2
templates ×2
.net ×1
c# ×1
c++-concepts ×1
c++20 ×1
constexpr ×1
dllimport ×1
extern ×1
marshalling ×1
typescript ×1