English 中文(简体)
挥发超负荷?
原标题:volatile overloading?

我听到,挥发性是超负荷工作等因素。

If a function is overloaded by volatile parameter, when is the volatile-version called?

我可以想象的是,当人们呼吁变幻莫测的转变时,情况就会出现。

最佳回答

挥发性可适用于参数,但它不是直接适用于参数时超负荷的因素。 然而,可以用它来区分参数的类型。 例如,这是合法的:

void f(int &p) {}; //reference to int
void f(volatile int &p) {}; //reference to volatile int

这并不是:

void f(int p) {};
void f(volatile int p) {};

其原因是,在第一个例子中,提及的内容不是不稳定的,而是愤怒的。 第二种情况是,这两种类型都是分类,因此都是相同的。

还有不稳定的方法。 它们等于宣布<代码>该为挥发性。 由于this 是个点,而不是包含类型本身,所以以下法律条文也是:

void c::f(int p) {};
void c::f(int p) volatile {};

这与通过<编码>const超载相同。

C++标准的这个相关部分是第13.1节可有效申报<>。 从C++11 起草N3290:

只在存在或不存在混凝土和(或)挥发性的情况下才有所不同的参数声明是等同的。 也就是说,在确定哪些职能被宣布、确定或要求时,忽视每个参数类型的最复杂和不稳定的字标。 [例:

typedef const int cInt;
int f(int);
int f(const int);          // redeclaration of f(int)
int f(int) { /* ... */ }   // definition of f(int)
int f(cInt) { /* ... */ }  // error: redefinition of f(int)

- 最终实例

只有参数类型最外层的汇合器和挥发型汇合器被忽略;在参数类型规格内掩埋的汇合器和挥发式汇合器具有重大意义,可用于区分超载功能申报。 尤其是,对于任何类型的T,pointer to T,pointer to const T, and pointer to挥发性T, as are considered separate paraile categories as are reference to T, reference to const T, and 参引自t

124) When a parameter type includes a function type, such as in the case of a parameter type that is a pointer to function, the const and volatile type-specifiers at the outermost level of the parameter type specifications for the inner function type are also ignored.

问题回答

例如:

#include <iostream>

struct A {
    void foo() {
        std::cout << "in non-volatile" << std::endl;
    }
    void foo() volatile {
        std::cout << "in volatile" << std::endl;
    }
};

int main()
{
    A a;
    a.foo();
    volatile A b;
    b.foo();
}

<代码>b.foo()将指volaful上下载。 如果struct A,ttt t有挥发的超载foo,b.foo(>将无效。

撰写测试方案,以便发现。

void func(const int& a)
{
    std::cout << "func(const)" << std::endl;
}

void func(const volatile int& a)
{
    std::cout << "func(const volatile)" << std::endl;
}

int main()
{
    const int a = 0;
    const volatile int b = 0;
    func(a);
    func(b);
    system("pause");
    return 0;
}

产出:

func(const)
func(const volatile)




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?