Proper way to define type (typedef vs #define)

Which of these methods is more secure for defining variable types? I know that we all frown when we see #defines, but it seems to work just as well as typedef here: Is there an advantage one way or the other, and if so what might it be? Method One: #include <iostream> #define byte unsigned char int main() { byte testByte = 'A'; std::cout << testByte

正确的方式来定义类型(typedef vs #define)

哪些方法对于定义变量类型更安全? 我知道当我们看到#define时,我们都皱着眉头,但它看起来和typedef一样工作: 这种方式有没有优势,如果有的话,它会是什么? 方法一: #include <iostream> #define byte unsigned char int main() { byte testByte = 'A'; std::cout << testByte << std::endl; return 0; } 方法二: #include <iostream>

Trouble understanding the C++11 syntax in the Rule of Zero

I am studying the Rule of Zero and have 2 questions for the final piece of code which demonstrates the rule. class module { public: explicit module(std::wstring const& name) : handle { ::LoadLibrary(name.c_str()), &::FreeLibrary } {} // other module related functions go here private: using module_handle = std::unique_ptr<void, decltype(&::

无法理解零规则中的C ++ 11语法

我正在研究零规则,并对最后一段代码有2个问题来说明规则。 class module { public: explicit module(std::wstring const& name) : handle { ::LoadLibrary(name.c_str()), &::FreeLibrary } {} // other module related functions go here private: using module_handle = std::unique_ptr<void, decltype(&::FreeLibrary)>; module_handle handle;

Using fully qualified names in C++

I am a C++ newcomer, trying to learn the language in parallel as I work on a project that requires it. I am using a fairly popular and stable open source library to do a lot of heavy lifting. Reading through the source, tutorials and code samples for the library, I have noticed that they always use fully qualified names when declaring types, which often results in very long and verbose lines wi

在C ++中使用完全限定的名称

我是一名C ++新手,试图在需要它的项目上学习语言。 我正在使用一个相当流行和稳定的开源库来做很多繁重的工作。 通过阅读库的源代码,教程和代码示例,我注意到它们在声明类型时总是使用完全限定的名称,这往往会导致很长且冗长的行,其中包含很多::。 这被认为是C ++中的最佳实践吗? 有没有不同的方式来处理这个问题? 他们可能发现比回答试用示例代码的人提出的许多问题要容易一些,因为他们没有“使用”涉及的命名空间

Strange declaration with using in C++

On a piece of code in a previous question in stackoverflow I saw this, strange to me, declaration with using : template <std::size_t SIZE> class A { public: ... using const_buffer_t = const char(&)[SIZE]; ... }; Could someone please address the following questions: What type it represents? Where do we need such kind of declarations? That's a type alias, a new syntax a

在C ++中使用奇怪的声明

在stackoverflow中的一个前面的问题中的一段代码中,我看到了这个,对我来说很奇怪, using声明: template <std::size_t SIZE> class A { public: ... using const_buffer_t = const char(&)[SIZE]; ... }; 有人可以解决以下问题吗? 它代表什么类型? 我们在哪里需要这种声明? 这是一个类型别名,它是自c ++ 11以来可用的新语法。 你实际上在做什么是typedefing数组的类型 const_buffer_t 将是

C++ 'typedef' vs. 'using ... = ...'

Possible Duplicate: What are the differences between typedef and using in C++11? The following code compiles and runs. My question is what is the difference between the "typedef" and "using" method for renaming the template specialization? template<typename T> struct myTempl{ T val; }; int main (int, char const *[]) { using templ_i = myTempl<int>;

C ++'typedef'与'using ... = ...'

可能重复: typedef和C ++ 11中使用的区别是什么? 以下代码编译并运行。 我的问题是“typedef”和“using”方法重新命名模板专业化有什么区别? template<typename T> struct myTempl{ T val; }; int main (int, char const *[]) { using templ_i = myTempl<int>; templ_i i; i.val=4; typedef myTempl<float> templ_f; templ_f f; f.val=5.3; return 0; } 编辑:

Import nested classes into namespace

Say I have a class like this: class A { public: class B { // ... }; static void f(); // ... }; I can refer to B as A::B and to f() as A::f() , but can I import B and f() into the global/current namespace? I tried using A::B; but that gave me a compilation error. You should be able to use namespace aliases for the class: using B = A::B; However you can't do tha

将嵌套类导入名称空间

假设我有这样的课程: class A { public: class B { // ... }; static void f(); // ... }; 我可以将B指向A::B ,将f()指向A::f() ,但是我可以将B和f()导入到全局/当前的命名空间吗? 我试过了 using A::B; 但是这给了我一个编译错误。 您应该可以使用该类的命名空间别名: using B = A::B; 然而,你不能用成员函数来做到这一点,即使使用静态成员函数也不行。 编辑:根据这个SO回答(C ++

What's the difference between using

This question already has an answer here: What is the difference between 'typedef' and 'using' in C++11? 4 answers The "using-style" was introduced to allow templated typedefs: template< typename T > using int_map = std::map< int, T >; You can not do this with typedef . I found it strange myself that it was decided to use using and not typedef as the

使用有什么区别

这个问题在这里已经有了答案: 在C ++ 11中'typedef'和'using'有什么区别? 4个答案 引入了“使用样式”以允许模板化的typedef: template< typename T > using int_map = std::map< int, T >; 你不能用typedef来做到这一点。 我发现自己很奇怪它决定使用using而不是typedef作为关键字,但我想委员会在扩展typedef语法时一定会发现一些问题。 我发现即使对于非模板,可读性也有很大提高: t

Return type of '?:' (ternary conditional operator)

Why does the first return a reference? int x = 1; int y = 2; (x > y ? x : y) = 100; While the second does not? int x = 1; long y = 2; (x > y ? x : y) = 100; Actually, the second did not compile at all - "not lvalue left of assignment". Expressions don't have return types, they have a type and - as it's known in the latest C++ standard - a value category. A conditio

'?:'的返回类型(三元条件运算符)

为什么第一个返回一个引用? int x = 1; int y = 2; (x > y ? x : y) = 100; 而第二个不? int x = 1; long y = 2; (x > y ? x : y) = 100; 实际上,第二个根本没有编译 - “不是左值任务”。 表达式没有返回类型,它们有一个类型,并且 - 正如它在最新的C ++标准中已知的 - 是一个值类别。 条件表达式可以是左值或右值。 这是它的价值类别。 (这有点简化,在C++11我们有左值,左值和右值)。 用非常宽泛和简单

how to determine whether float or double is required?

Given a real value, can we check if a float data type is enough to store the number, or a double is required? I know precision varies from architecture to architecture. Is there any C/C++ function to determine the right data type? For background, see What Every Computer Scientist Should Know About Floating-Point Arithmetic Unfortunately, I don't think there is any way to automate the d

如何确定是否需要float或double?

给定一个真正的价值,我们可以检查一个float数据类型是否足以存储该数字,或者是否需要double ? 我知道精度因架构而异。 是否有任何C / C ++函数来确定正确的数据类型? 有关背景知识,请参阅每位计算机科学家应了解的浮点运算 不幸的是,我不认为有什么方法可以使决策自动化。 通常,当人们用浮点数表示数字而不是字符串时,其意图是用数字进行算术运算。 即使所有输入符合给定浮点类型并具有可接受的精度,您仍然必

union consisting of float : completely insane output

#include <stdio.h> union NumericType { float value; int intvalue; }Values; int main() { Values.value = 1094795585.00; printf("%f n",Values.value); return 0; } This program outputs as : 1094795648.000000 Can anybody explain Why is this happening? Why did the value of the float Values.value increase? Or am I missing something here? First off, this has nothing what

由浮点组成的联合:完全疯狂的输出

#include <stdio.h> union NumericType { float value; int intvalue; }Values; int main() { Values.value = 1094795585.00; printf("%f n",Values.value); return 0; } 该计划输出如下: 1094795648.000000 有谁可以解释为什么会发生这种情况? 为什么float Values.value的值增加? 或者我在这里错过了什么? 首先,这与使用union没有任何关系。 现在,假设你写: int x = 1.5; printf("