在C或C++中,函数声明和函数签名有什么区别?
我知道一些函数声明,但函数签名对我来说是全新的.有功能签名概念有什么意义?实际使用的两个概念是什么?
谢谢!
我做了一些快速搜索,无法找到答案.
我很想知道为什么在Objective-C中,它id被用作init方法的返回类型.
我的猜测是因为如果类被重写,你不想返回超类的类型的对象,但我有兴趣知道它是否由于其他原因而完成.
任何人都可以总结一下函数模板重载的想法吗?重要的是,模板参数或功能参数?返回值怎么样?
例如,给定一个功能模板
template<typename X, typename Y> void func(X x, Y y) {}
什么是重载的功能模板?
1) template<typename X> void func(X x, int y) {}
2) template<typename X, typename Y> X func(X x, Y y) {}
3) template<class X, class Y, class Z> void func(X x, Y y, Z z) {}
在Josuttis和Vandevoorde着名的模板书C++模板:完整指南中,他们讨论了有关函数模板重载的细节.
在他们的一个示例中,与函数签名和重载函数模板的讨论相关,它们呈现了他们用以下术语描述的代码:
This program is valid and produces the following output:
(Note: Output shown below)
但是,当我在Visual Studio 2010中构建和编译相同的代码时,我得到了不同的结果.这让我相信VS 2010编译器产生的代码不正确,或者Josuttis代码有效是不正确的.
这是代码.(Josuttis 2003,Section 12.2.1)
// File1.cpp
#include <iostream>
template<typename T1, typename T2>
void f1(T2, T1)
{
    std::cout << "f1(T2, T1)" << std::endl;
}
extern void g();
int main()
{
    f1<char, char>('a', 'b');
    g();
}
...
// File2.cpp
#include <iostream>
template<typename T1, typename T2>
void f1(T1, T2)
{
    std::cout << "f1(T1, T2)" << std::endl;
}
void g()
{
    f1<char, char>('a', 'b'); …也许我有两个具有相同功能名称和参数的接口,但具有不同的返回值:
struct A { virtual void foo() = 0; };
struct B { virtual int foo() = 0; };
如何定义继承此接口的类C(如果可能的话)?例如,我写了一些未编译的伪代码:
// this code is fake, it doesn't compiled!!
struct C : A, B
{
    // how to tell compiler what method using if referenced from C?
    using void foo();  // incorrect in VS 2012
    // and override A::foo() and B::foo()?
    virtual void foo() { std::cout << "void C::foo();\n"; } // incorrect
    virtual int foo() { std::cout << "int C::foo();\n"; return …以下代码是在g ++ 4.1.2和g ++ 4.4.4上编译的.两者都给出了评论中指出的结果.
int f(const int * a)
{
   return 0;
}
template<typename A>
int f(A a)
{
   return 1;
}
int main()
{
    int x;
    // return f(&x); // returns 1
    return f((const int *)&x); // returns 0
}
它似乎归结为做出f(int *)决议f<int *>(int *)而不是预期的召唤f(const int *).我发现这令人震惊,完全不直观.  
这是g ++中的一个错误,是C++的一个黑暗角落,还是由于某种原因我很遗憾?如果它不是一个错误,它背后的理论或逻辑是什么?有关此问题的安全措施吗?
我有一个使用返回码的类:
class MyClass
{
    // ...
public:
    // ValueType get_value() const;               // usual code
    ErrorCode get_value(ValueType& value) const;  // uses error code
   // ...
};
因此,第二种形式get_value()实际上将值作为函数参数提供,而不是作为返回值.
是否有可能推断出函数参数的类型get_value(),也许使用   decltype?
int main()
{
    // ...
    MyClass my_class;
    // auto val = my_class.get_value();  // okay: value has correct type
    declytype( /* something here */ ) value;
    const auto error = my_class.get_value( value );
    // ...
}
我在声明一个使用 的函数时遇到一些麻烦boost::enable_if:下面的代码给了我一个编译器错误:
// Declaration
template <typename T>
void foo(T t);
// Definition
template <typename T>
typename boost::enable_if<boost::is_same<T, int> >::type foo(T t)
{
}
int main()
{
    foo(12);
    return 0;
}
编译时,出现“对 foo 的调用不明确”错误。根据 的定义enable_if,“type”typedef 对应于void条件为 true 时,因此据我所知,两个签名匹配foo。为什么编译器认为它们不同,是否有正确的方法来转发声明foo(最好不要重复该enable_if部分)?
GCC将这两个函数声明视为等效:
void F(int* a) { }
void F(int* const a) { }
test.cpp:在函数'void F(int*)'中:
test.cpp:235:错误:重新定义'void F(int*)'
test.cpp:234:错误:'void F(int*)'在此处定义
这有一定意义,因为在这种情况下调用者总是会忽略const ...它只会影响函数内部参数'a'的使用.
我想知道的是标准在哪里(如果有的话)说,为了重载解析的目的,丢弃用作函数参数的指针的限定符是特别好的.
(我真正的问题是我想弄清楚GCC在内部剥离这些毫无意义的限定符的地方,并且由于GCC的C++前端充斥着引用标准的注释,标准的相关部分可能帮助我找到正确的位置. )
我最近了解到,构造函数在 C++ 中没有名称以及有关它们的其他一些信息。我还知道函数在 C++ 中有一种称为函数类型的类型。例如,
void func(int)
{
}
在上面的代码片段中, 的函数类型func为。 void (int)
现在,我想知道,由于构造函数是特殊的成员函数,那么它们是否也具有如上所示的类型。例如说我们有:
struct Name
{ 
    Name(int)
    {
    }
};
上面所示的构造函数是否也像普通函数或普通成员函数一样具有函数类型。如果是,那么我们如何找到该类型。就像我们可以decltype在普通函数上使用一样,是否允许decltype在构造函数上使用来查找它们的类型。
c++ ×9
function ×2
templates ×2
ambiguity ×1
c ×1
constructor ×1
declaration ×1
decltype ×1
enable-if ×1
objective-c ×1
overloading ×1
signature ×1
standards ×1
types ×1