Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 忽略了部分函数专门化中值类型的顶级常量限定符_C++_C++11 - Fatal编程技术网

C++ 忽略了部分函数专门化中值类型的顶级常量限定符

C++ 忽略了部分函数专门化中值类型的顶级常量限定符,c++,c++11,C++,C++11,由于常量int专门化,出现以下错误: #include <iostream> using std::cout; using std::endl; template <typename T> void g(T val) { cout << "unknown" << endl; } template <> void g(int && val) { cout << "int &&

由于常量int专门化,出现以下错误:

#include <iostream>
using std::cout;
using std::endl;

template <typename T> void g(T val)
{
    cout << "unknown" << endl;
}

template <> void g(int && val)
{
    cout << "int &&" << endl;
}

template <> void g(const int && val)
{
    cout << "const int &&" << endl;
}

template <> void g(int & val)
{
    cout << "int &" << endl;
}

template <> void g(const int & val)
{
    cout << "const int &" << endl;
}

template <> void g(int val)
{
    cout << "int" << endl;
}

template <> void g(const int val)  //redefinition here
{
    cout << "const int" << endl;
}

int main() {}

error: redefinition of 'g'
template <> void g(const int val)
                 ^

为什么T&和T&与const T&和const T&不同,但T与const T没有区别?

因为函数参数的顶级常量是函数的一个实现细节。例如,以下内容是有效的:

// Prototype
void foo(int c);

// Implementation
void foo(int const c) { ... }
因为参数是按值传递的,所以调用方并不真正关心函数是否要修改自己的私有副本。因此,顶级常量不是函数签名的一部分


请注意,这仅适用于顶级常量!int和int-const在函数原型中是等价的,int*和int*const也是等价的。但是int*和int const*不是。因为函数参数的顶级常量是函数的一个实现细节。例如,以下内容是有效的:

// Prototype
void foo(int c);

// Implementation
void foo(int const c) { ... }
因为参数是按值传递的,所以调用方并不真正关心函数是否要修改自己的私有副本。因此,顶级常量不是函数签名的一部分


请注意,这仅适用于顶级常量!int和int-const在函数原型中是等价的,int*和int*const也是等价的。但是int*和int const*并非如此。

使用参数时,需要考虑以下几点:a:不通过引用传递参数是在创建自己的新变量,B:通过引用传递参数是在使用与参数同名的同一变量

这很重要,因为:

void doStuff (const int x)
{
    //x is its own constant variable
}
鉴于

void doStuff (const int& x)
{
    //because x is the same variable that was used when calling this function
    //x can be modified outside of this function, but not inside the function
    //due to the const modifier
}
第二个函数上的const修饰符允许您执行以下操作:

int main ()
{
    const int x = 10;
    doStuff(x)
}
引用用于修改另一个函数中的变量并在堆栈上保存内存,这会节省内存,因为它使用指针而不是新创建的变量,因此任何大于int的变量都将通过使用引用调用来节省内存,即使您没有在函数中修改它


现在,如果我是正确的,&&运算符不应该在参数中使用,因为这是一个布尔运算符,不会修改参数类型。它只应在条件下使用,但在编译编译器认为它是[type]类型的引用时不会产生语法错误&但这对变量的使用方式没有任何影响,除了计算机处理时间稍长之外。

使用参数时,有几点需要考虑,A:不通过引用传递参数将创建它自己的新变量,B:通过引用传递参数将使用与参数相同的变量,并使用不同的名称

这很重要,因为:

void doStuff (const int x)
{
    //x is its own constant variable
}
鉴于

void doStuff (const int& x)
{
    //because x is the same variable that was used when calling this function
    //x can be modified outside of this function, but not inside the function
    //due to the const modifier
}
第二个函数上的const修饰符允许您执行以下操作:

int main ()
{
    const int x = 10;
    doStuff(x)
}
引用用于修改另一个函数中的变量并在堆栈上保存内存,这会节省内存,因为它使用指针而不是新创建的变量,因此任何大于int的变量都将通过使用引用调用来节省内存,即使您没有在函数中修改它


现在,如果我是正确的,&&运算符不应该在参数中使用,因为这是一个布尔运算符,不会修改参数类型。它只应在条件下使用,但在编译编译器认为它是[type]类型的引用时不会产生语法错误&但这对变量的使用方式没有任何影响,只是计算机处理的时间稍微长了一点,谢谢。这适用于复制对象的任何时候。顶级常量将被忽略,因为复制自对象不会更改。例如const int ci;int i;i=ci;但是请注意,您可以说模板void gconst int val{},它是有效的。只有函数类型才是voidint,因此,由于函数类型用于推断T,T将变为int。但是,如果您显式地这样提供它,它会起作用。@johanneschaub是的,但是为了使用它,我必须在函数调用中显式地指定模板类型。int i=1;const int ci=i;全球通讯基础设施;否则gci;好的,谢谢。这适用于复制对象的任何时候。顶级常量将被忽略,因为复制自对象不会更改。例如const int ci;int i;i=ci;但是请注意,您可以说模板void gconst int val{},它是有效的。只有函数类型才是voidint,因此,由于函数类型用于推断T,T将变为int。但是,如果您显式地这样提供它,它会起作用。@johanneschaub是的,但是为了使用它,我必须在函数调用中显式地指定模板类型。int i=1;const int ci=i;全球通讯基础设施;否则gci;调用VoIDIt.C++编译是上下文敏感的,因此编译器在函数参数列表的上下文中看到和。在C++ 11兼容编译器中,这意味着参数是R值引用类型。C++编译是上下文敏感的,因此编译器在函数参数列表中看到和。在符合c++11的环境中 编译器这表示参数是r值引用类型。