标签: function-signature

C++和覆盖问题

MyClass* const Func(const std::string& statename)
Run Code Online (Sandbox Code Playgroud)

因为这个封面是错误的

返回类型的解析警告(PW.USELESS_TYPE_QUALIFIER_ON_RETURN_TYPE)类型限定符无意义.

我们真的需要在这里删除const吗?

c++ static-analysis coding-style coverity-prevent function-signature

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

Tap的函数签名(K-combinator)

我在一本书中读到过tap函数(也称为K-Combinator)的函数签名如下:

tap :: (a -> *) -> a -> a
Run Code Online (Sandbox Code Playgroud)

“该函数接受一个输入对象 a 和一个对 a 执行某些操作的函数。它使用提供的对象运行给定的函数,然后返回该对象。”

  1. 有人能帮我解释一下函数签名中星号(*)是什么意思吗?
  2. 下面的实现是否正确?
  3. 如果这三个实现都正确,那么应该在什么时候使用哪一个?有什么例子吗?

实施1:

const tap = fn => a => { fn(a); return a; };

tap((it) => console.log(it))(10); //10
Run Code Online (Sandbox Code Playgroud)

实施2:

const tap = a => fn => { fn(a); return a; }; 

tap(10)((it) => console.log(it)); //10
Run Code Online (Sandbox Code Playgroud)

实施3:

const tap = (a, fn) => {fn(a); return a; };

tap(10, (it) => console.log(it)); //10
Run Code Online (Sandbox Code Playgroud)

javascript functional-programming function-signature k-combinator

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

派生类不会重写具有不同签名的虚函数

我有一个派生类,我希望其中一个函数覆盖其在基类中的版本,但具有不同的签名。简单的例子:

#include "stdio.h"

bool use_foo = false;

class Foo {
public:
    virtual int func(double x) { printf ("%f in Foo!\n", x); }
};

class Bar : public Foo {
public:
    int func(short x) { printf ("%d in Bar!\n", x); }
};

int main () {
    Foo* A;
    if (use_foo) 
        A = new Foo;
    else 
        A = new Bar;

    A->func(2);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

即使 A 被分配为派生类,上面的代码也会调用基类副本:

> g++ test.cpp -o test -O3 && ./test
2.000000 in Foo!
Run Code Online (Sandbox Code Playgroud)

因为(据我的理解)参数可以转换为匹配基类签名,并且派生类不会因为这种差异而覆盖它(但在这种情况下它不会隐藏它吗?)。如果我将基类函数更改short …

c++ overriding virtual-functions function-signature

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

TypeScript - 检查对象的属性是否是具有给定签名的函数

我有一个从对象获取属性的函数。

// Utils.ts
export function getProperty<T, K extends keyof T>(obj: T, key: string): T[K] {
    if (key in obj) { return obj[key as K]; }
    throw new Error(`Invalid object member "${key}"`);
}
Run Code Online (Sandbox Code Playgroud)

我想检查返回的属性是否是具有给定签名的函数,然后使用提供的参数调用该属性。

getProperty()用于动态获取对象的方法之一并调用它。我试过:

let property: this[keyof this] = utils.getProperty(this, name);
if (typeof property === 'function') ) { property(conf); }
Run Code Online (Sandbox Code Playgroud)

但这给出了“无法调用类型缺少调用签名的表达式。类型'any'没有兼容的调用签名。” 错误。我知道来自的属性getProperty()确实可以是任何类型,但如何确定它是带有(conf: {}): void签名的函数?

typechecking function-signature typescript

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

什么时候从函数的参数化中省略 const 是安全的?

很多时候,我编写 C++ 函数时,似乎const比其他人使用了更多的修饰符。

例如,我编写 Excel .xlsx 文件,并为此使用 LibXL 库,并且文档提到了这样的函数:

bool writeNum(int row, int col, double value, Format* format = 0)
Run Code Online (Sandbox Code Playgroud)

我碰巧继承了这个函数,我的签名如下:

bool XLAPIENTRY writeNum(const int row, const int col, const double value, IFormatT<wchar_t> * format = 0);
Run Code Online (Sandbox Code Playgroud)

请注意const int行和列的 s。

const问题:省略s安全吗int?(很明显,对于指针来说,忽略指针的 const 是危险的,但是做一些 an 的本地副本int并不是那么危险)。请注意,包含 writeNum 函数的原始标头int也没有提到 const-ing s。为什么这里const省略了修饰符?

c++ constants parameter-passing function-signature

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

为什么名字错误不打破我的程序?

可能重复:
在C++中是否重载了main()?

这是我的代码:

#include <iostream>

int main(void* a, void* b)
{
    std::cout << "hello standalone " << std::endl;                      
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

名称重整后,不同的参数应该有不同的符号名称(void* a, void* b)应该不同(int, char**),但是这个程序在运行时没有任何问题.

这是为什么?

c++ program-entry-point function-signature

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

C++ 创建头文件时,多个重载函数实例与参数列表匹配

在我的程序的主文件中,我有以下声明

int main()
{
    Customer c;
    Part p;
    Builder b;
    auto partsVec =  readpartFile();
    auto customerVec = readcustomerFile();
    auto builderVec = readbuilderFile();

    fexists("Parts.txt");
    complexity(c, partsVec);
    robotComplexity(partsVec,customerVec);
    writeFile(buildAttempt(b, complexity(c, partsVec), variability(customerVec, builderVec)));
}
Run Code Online (Sandbox Code Playgroud)

我的头文件由以下内容组成

vector<Part> readpartFile();

vector<Customer> readcustomerFile();

vector<Builder> readbuilderFile();

float complexity(const Customer& c, const std::vector<Part>& parts);

void robotComplexity(vector<Part> vecB, vector<Customer> vecC);

double variability(const vector<Customer>& customerList, const vector<Builder>& builderList);

vector<double> buildAttempt(Builder b, double variaiblity, double complexityRobot);

void writeFile(vector<double> build);
Run Code Online (Sandbox Code Playgroud)

除了 robotsComplexity 之外,所有功能都链接在一起。我在 main 中声明此函数会产生以下错误。

重载函数“robotComplexity”的多个实例与参数列表匹配: -- function "robotComplexity(const std::vector> &parts, const …

c++ linker-errors header-files forward-declaration function-signature

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