精华内容
下载资源
问答
  • Filip Ekberg解释了实时系统近实时系统之间的区别,以及如何使用SignalR构建近实时系统 入门 如果您具有Visual Studio Pro或更高版本,请使用DNC___Demos _Pro.sln一次打开所有项目。 如果您拥有Express版本,请...
  • I know that in C++11 we can now use using to write type alias, like typedef s: 我知道在C ++ 11中,我们现在可以使

    本文翻译自:What is the difference between 'typedef' and 'using' in C++11?

    I know that in C++11 we can now use using to write type alias, like typedef s: 我知道在C ++ 11中,我们现在可以使用using来编写类型别名,例如typedef

    typedef int MyInt;
    

    Is, from what I understand, equivalent to: 据我了解,相当于:

    using MyInt = int;
    

    And that new syntax emerged from the effort to have a way to express " template typedef ": 并且,这种新语法是通过努力表达“ template typedef ”的方式而出现的:

    template< class T > using MyType = AnotherType< T, MyAllocatorType >;
    

    But, with the first two non-template examples, are there any other subtle differences in the standard? 但是,对于前两个非模板示例,标准中是否还有其他细微差别? For example, typedef s do aliasing in a "weak" way. 例如, typedef会以“弱”方式进行别名。 That is it does not create a new type but only a new name (conversions are implicit between those names). 也就是说,它不会创建新的类型,而只会创建一个新名称(这些名称之间的转换是隐式的)。

    Is it the same with using or does it generate a new type? 是否与using相同或会生成新类型? Are there any differences? 有什么区别吗?


    #1楼

    参考:https://stackoom.com/question/j5zm/C-中的-typedef-和-using-有什么区别


    #2楼

    They are equivalent, from the standard (emphasis mine) (7.1.3.2): 它们与标准(强调我的)(7.1.3.2)是等效的:

    A typedef-name can also be introduced by an alias-declaration. typedef名称也可以通过别名声明来引入。 The identifier following the using keyword becomes a typedef-name and the optional attribute-specifier-seq following the identifier appertains to that typedef-name. using关键字后面的标识符变为typedef名称,而标识符后面的可选attribute-specifier-seq属于该typedef名称。 It has the same semantics as if it were introduced by the typedef specifier. 它具有与typedef说明符引入的语义相同的语义。 In particular, it does not define a new type and it shall not appear in the type-id. 特别是,它没有定义新的类型,也不应出现在type-id中。


    #3楼

    The using syntax has an advantage when used within templates. 在模板中使用时, using语法具有优势。 If you need the type abstraction, but also need to keep template parameter to be possible to be specified in future. 如果需要类型抽象,还需要保留模板参数,以便将来可以指定。 You should write something like this. 你应该这样写。

    template <typename T> struct whatever {};
    
    template <typename T> struct rebind
    {
      typedef whatever<T> type; // to make it possible to substitue the whatever in future.
    };
    
    rebind<int>::type variable;
    
    template <typename U> struct bar { typename rebind<U>::type _var_member; }
    

    But using syntax simplifies this use case. 但是使用语法可以简化此用例。

    template <typename T> using my_type = whatever<T>;
    
    my_type<int> variable;
    template <typename U> struct baz { my_type<U> _var_member; }
    

    #4楼

    They are largely the same, except that: 它们基本相同,除了:

    The alias declaration is compatible with templates, whereas the C style typedef is not. 别名声明与模板兼容,而C风格的typedef不兼容。


    #5楼

    They are essentially the same but using provides alias templates which is quite useful. 它们本质上是相同的,但是using提供了非常有用的alias templates One good example I could find is as follows: 我可以找到一个很好的例子,如下所示:

    namespace std {
     template<typename T> using add_const_t = typename add_const<T>::type;
    }
    

    So, we can use std::add_const_t<T> instead of typename std::add_const<T>::type 因此,我们可以使用std::add_const_t<T>代替typename std::add_const<T>::type


    #6楼

    I know the original poster has a great answer, but for anyone stumbling on this thread like I have there's an important note from the proposal that I think adds something of value to the discussion here, particularly to concerns in the comments about if the typedef keyword is going to be marked as deprecated in the future, or removed for being redundant/old: 我知道原始发帖人的回答很好,但是对于像我这样绊脚石的任何人,提案中都有一条重要说明,我认为这对此处的讨论特别是对typedef关键字是否在注释中的关注增加了一些价值。将来将被标记为已弃用,或由于冗余/旧而被删除:

    It has been suggested to (re)use the keyword typedef ... to introduce template aliases: 已建议(重新)使用关键字typedef ...引入模板别名:

     template<class T> typedef std::vector<T, MyAllocator<T> > Vec; 

    That notation has the advantage of using a keyword already known to introduce a type alias. 该符号的优点是使用已知的关键字来引入类型别名。 However, it also displays several disavantages [sic] among which the confusion of using a keyword known to introduce an alias for a type-name in a context where the alias does not designate a type, but a template; 但是,它也显示了一些不足之处,其中在别名不是指定类型而是模板的情况下,使用已知为类型名称引入别名的关键字会造成混淆。 Vec is not an alias for a type, and should not be taken for a typedef-name. Vec 不是类型的别名,并且不应将其用作typedef名称。 The name Vec is a name for the family std::vector<•, MyAllocator<•> > – where the bullet is a placeholder for a type-name.Consequently we do not propose the “typedef” syntax.On the other hand the sentence Vec名称是std::vector<•, MyAllocator<•> >家族的名称–项目符号是类型名称的占位符。因此,我们不建议使用“ typedef”语法。句子

     template<class T> using Vec = std::vector<T, MyAllocator<T> >; 

    can be read/interpreted as: from now on, I'll be using Vec<T> as a synonym for std::vector<T, MyAllocator<T> > . 可以理解为: 从现在开始,我将使用Vec<T>作为std::vector<T, MyAllocator<T> >的同义词 With that reading, the new syntax for aliasing seems reasonably logical. 通过阅读,别名的新语法似乎是合理的。

    To me, this implies continued support for the typedef keyword in C++ because it can still make code more readable and understandable . 对我来说,这意味着将继续支持C ++中的typedef关键字,因为它仍然可以使代码更具可读性和可理解性

    Updating the using keyword was specifically for templates, and (as was pointed out in the accepted answer) when you are working with non-templates using and typedef are mechanically identical, so the choice is totally up to the programmer on the grounds of readability and communication of intent. 更新using关键字是专门为模板,并(在接受的答案指出)当你与非模板工作usingtypedef是机械上相同的,所以在选择完全取决于可读性的理由和程序员目的沟通。

    展开全文
  • using System; using System.ComponentModel; // The following example demonstrates how to use the // GC.SuppressFinalize method in a resource class to prevent // the clean-up code for th...

    using System;
    using System.ComponentModel;

    // The following example demonstrates how to use the
    // GC.SuppressFinalize method in a resource class to prevent
    // the clean-up code for the object from being called twice.

    public class DisposeExample
    {
        // A class that implements IDisposable.
        // By implementing IDisposable, you are announcing that
        // instances of this type allocate scarce resources.
        public class MyResource: IDisposable
        {
            // Pointer to an external unmanaged resource.
            private IntPtr handle;
            // Other managed resource this class uses.
            private Component component = new Component();
            // Track whether Dispose has been called.
            private bool disposed = false;

            // The class constructor.
            public MyResource(IntPtr handle)
            {
                this.handle = handle;
            }

            // Implement IDisposable.
            // Do not make this method virtual.
            // A derived class should not be able to override this method.
            public void Dispose()
            {
                Dispose(true);
                // This object will be cleaned up by the Dispose method.
                // Therefore, you should call GC.SupressFinalize to
                // take this object off the finalization queue
                // and prevent finalization code for this object
                // from executing a second time.
                GC.SuppressFinalize(this);//问题2:在“Dispose()”中“GC.SuppressFinalize(this);”是什么意思?
            }

            // Dispose(bool disposing) executes in two distinct scenarios.
            // If disposing equals true, the method has been called directly
            // or indirectly by a user's code. Managed and unmanaged resources
            // can be disposed.
            // If disposing equals false, the method has been called by the
            // runtime from inside the finalizer and you should not reference
            // other objects. Only unmanaged resources can be disposed.
            private void Dispose(bool disposing)
            {
                // Check to see if Dispose has already been called.
                if(!this.disposed)
                {
                    // If disposing equals true, dispose all managed
                    // and unmanaged resources.
                    if(disposing)
                    {
                        // Dispose managed resources.
                        component.Dispose();//问题1:也就是为什么GC垃圾回收机制在回收对象的时候只回收或者释放非托管资源,而不回收托管资源?
                    }
            
                    // Call the appropriate methods to clean up
                    // unmanaged resources here.
                    // If disposing is false,
                    // only the following code is executed.
                    CloseHandle(handle);
                    handle = IntPtr.Zero;           
                }
                disposed = true;        
            }

            // Use interop to call the method necessary 
            // to clean up the unmanaged resource.
            [System.Runtime.InteropServices.DllImport("Kernel32")]
            private extern static Boolean CloseHandle(IntPtr handle);

            // Use C# destructor syntax for finalization code.
            // This destructor will run only if the Dispose method
            // does not get called.
            // It gives your base class the opportunity to finalize.
            // Do not provide destructors in types derived from this class.
            ~MyResource()     
            {
                // Do not re-create Dispose clean-up code here.
                // Calling Dispose(false) is optimal in terms of
                // readability and maintainability.
                Dispose(false);//问题1:为什么在析构函数中调用的是“Dispose(false);”?
            }
        }

        public static void Main()
        {
            // Insert code here to create
            // and use a MyResource object.
        }
    }

    产生了两个问题(代码中红色标注区域):

    问题1:为什么在析构函数中调用的是“Dispose(false);”,也就是为什么GC垃圾回收机制在回收对
    象的时候只回收或者释放非托管资源,而不回收托管资源?
    问题2:在“Dispose()”中“GC.SuppressFinalize(this);”是什么意思?

    上网搜了写资料,终于了解了这种设计的用意和优点:
    参考资料:
    http://baike.baidu.com/view/4471636.htm
    http://www.360doc.com/content/11/0503/18/6075898_114106056.shtml

    解答问题1:
    在.NET的对象中实际上有两个用于释放资源的函数:Dispose和Finalize。Finalize的目的是用于释放非托管的资源,而Dispose是用于释放所有资源,包括托管的和非托管的。
    在这个模式中,通过一个变量“disposing”来区分是客户调用(true)还是GC调用(false)。
    这是因为,Dispose()函数是被其它代码显式调用并要求释放资源的,而Finalize是被GC调用的。
    在GC调用的时候MyResource所引用的其它托管对象(component)可能还不需要被销毁,并且即使
    要销毁,也会由GC来调用。因此在Finalize中只需要释放非托管资源即可。

    解答问题2:
    由于在Dispose()中已经释放了托管和非托管的资源,因此在对象被GC回收时再次调用Finalize是
    没有必要的,所以在Dispose()中调用GC.SuppressFinalize(this)避免重复调用Finalize。

    因此,上面的模式保证了:
    1、 Finalize只释放非托管资源;
    2、 Dispose释放托管和非托管资源;
    3、 重复调用Finalize和Dispose是没有问题的;
    4、 Finalize和Dispose共享相同的资源释放策略,因此他们之间也是没有冲突的。
    在C#中,这个模式需要显式地实现,其中C#的~MyResource()函数代表了Finalize()。

    优点:
    1、如果客户没有调用Dispose(),未能及时释放托管和非托管资源,那么在垃圾回收时,还有机会执
    行Finalize(),释放非托管资源,但是造成了非托管资源的未及时释放的空闲浪费。
    2、如果客户调用了Dispose(),就能及时释放了托管和非托管资源,那么该对象被垃圾回收时,不回
    执行Finalize(),提高了非托管资源的使用效率并提升了系统性能。

    此外还有Close()方法,此方法一般和Open()方法配合来使用,对于数据库连接,一般可以逆向操作,比如打开->关闭,关闭->打开,而对于文件操作,一般是关闭文件,相当于Dispose(),而且有的用户更愿意使用Close()来释放资源,所以出现了一下这种相当于适配器模式的代码:

     

    public void Close()

    {

    Dispose(();

    }

    最后还要再说一点,据MSDN上说C#不允许类实现Finalize()方法,所以用析构函数来代替。

    转载于:https://www.cnblogs.com/fengye87626/archive/2012/12/17/2821304.html

    展开全文
  • -pthread -lpthread区别就是少了个l, 教材都是在编译链接时候要求加下-lpthread 解决链接问题 gcc官方说明是 -pthread Define additional macros required for using the POSIX threads library. You ...

    -pthread 和-lpthread区别就是少了个l, 教材都是在编译的链接的时候要求加下-lpthread 解决链接的问题

    gcc的官方说明是

    -pthread
    
    Define additional macros required for using the POSIX threads library. You should use this option consistently for both compilation and linking. This option is supported on GNU/Linux
    
    targets, most other Unix derivatives, and also on x86 Cygwin and MinGW targets.
    

    就是相对于-lpthread 多定义了一些编译宏,

    使用下面的方法 导出下默认的编译宏,对比了下, 发现多了个_REENTRANT

    ubuntu@ubuntu-nvidia:~$ gcc -dM -E -pthread - </dev/null > pthread
    ubuntu@ubuntu-nvidia:~$ gcc -dM -E - </dev/null > nopthread
    ubuntu@ubuntu-nvidia:~$ diff pthread nopthread
    105d104
    < #define _REENTRANT 1
    

    这个宏的作用如下

    Macro: _REENTRANT
    
    These macros are obsolete. They have the same effect as defining _POSIX_C_SOURCE with the value 199506L.
    
    Some very old C libraries required one of these macros to be defined for basic functionality (e.g. getchar) to be thread-safe.
    

    这个宏可以保证一些函数是线程安全的,但是现在相当于是一个兼容性的作用,在 1995 edition of the POSIX.1c standard (IEEE Standard 1003.1c-1995) 以前你可能需要加下。

    To compile a single-threaded application, define neither the -D_REENTRANT nor the -D_POSIX_C_SOURCE flag. When these flags are not present, all the old definitions for errno, stdio, and so on, remain in effect.
    

    有些年头了,像errno现在早就是函数了

    总结:

    对现在版本的编译器和c库没啥区别,对老古董,多线程的时候你最好使用-pthread, 他可以让你的代码更安全。

    展开全文
  • typeofGetType的区别

    2015-05-27 14:35:00
    http://stackoverflow.com/questions/4537945/what-is-the-difference-of-getting-type-by-using-gettype-and-typeof ...You can only use typeof() when you know that type at compile time, and you're trying t...

    http://stackoverflow.com/questions/4537945/what-is-the-difference-of-getting-type-by-using-gettype-and-typeof

     

    You can only use typeof() when you know that type at compile time, and you're trying to obtain the corresponding Type object. (Although the type could be a generic type parameter, e.g. typeof(T) within a class with a type parameter T.) There don't need to be any instances of that type available to use typeof. The operand for typeof is always the name of a type or type parameter. It can't be a variable or anything like that.

    Now compare that with object.GetType(). That will get the actual type of the object it's called on. This means:

    • You don't need to know the type at compile time (and usually you don't)
    • You do need there to be an instance of the type (as otherwise you have nothing to call GetType on)
    • The actual type doesn't need to be accessible to your code - for example, it could be an internal type in a different assembly

    One odd point: GetType will give unexpected answers on nullable value types due to the way that boxing works. A call to GetType will always involve boxing any value type, including a nullable value type, and the boxed value of a nullable value type is either a null reference or a reference to an instance of a non-nullable value type.

    转载于:https://www.cnblogs.com/chucklu/p/4533389.html

    展开全文
  • Jquery.ajax()中的参数typemethod的区别

    万次阅读 2018-11-30 20:51:16
    You should use type if you’re using versions of jQuery prior to 1.9.0. 即typemethod是一样。只不过是1.9.0版本出method 估计是让程序员更好记,更符合习惯吧。(个人也是认为meth...
  • 在使用VS2013时报错 fopenfopen_s的区别 Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. fopen函数: 原型:FILE * fopen(const char * ...
  • <p>The golang docs says that <p>Seed, unlike the ...<p>We are using the same seed in two different threads, so is such using of <code>rand.Seed</code> considered to be a thread non-safe? </div>
  • deletedelete[]的区别

    2009-09-24 13:54:00
    deletedelete[]都可以删除已经分配内存,但是两者有所不同。例如:程序1#include using namespace std; int main() { char *str=new char[4]; delete [] str;//or use delete str; return 0; }程序2#include ...
  • <p>I can use GET to send and get data from the server using this VBScript example: <pre><code>Send "https://www.server.com/send.php" Sub Send(url) Dim objHTTP, MyResponse Set objHTTP = CreateObject...
  • really throwing me is: how does <code>app.js</code> know I'm refering to folders in the <code>node_modules</code> folder and how does <code>app.css</code> know I'm refering to Bootstrap just by using ...
  • Is it safer to use variables always instead of putting them in directly? <p>By using the following on a username field: <pre><code>$var = trim($_POST['username']); $var = preg_replace('/[^0-9a-zA-Z...
  • I'm trying to understand why we use generate in verilog along with a for loop.Using a generate and for loop together:reg [3:0] temp; genvar i; generate for (i = 0; i &lt; 3 ; i = i + 1) begin: ...
  • The maintainers of gulp and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications....
  • Well, I know its a bit stupid. If I want to store a large amount of ... Should I use xml or database(mysql)?Why do you choose (that) one?What are the pros and cons using xml?What are the pros and co...
  • <p>I've been using go for a short time now, and I've been noticing that there are duplicate packages between Go (standard library) and golang.org/x/. <p>My questions are: Why are they released twice?...
  • <p>I'd like to know the difference in PHP when using either <code>return</code> or <code>echo</code> as reading on Laravel forums tends to contradict points made in other questions such as this: ...
  • <p>In Java the same thing can be done using <pre><code>for (int e : array) { System.out.println(e); } </code></pre> <p>is there any difference between the above 2 code segments </div>
  • <p>I would use pointers to implement a chained struct but is this the only case we have to use pointers in struct in order to gain performance? <p>PS: in the above struct we talk about a simple int ...
  • <p>So I have this struct in Go: <pre><code>type Car struct { Name string `json:"name"` ...<p>But I get a very ugly code when using this values, I have to do pointer dereferencing everywhere </div>
  • Is there a preference to use one technique over the other? The <a href="https://golang.org/pkg/sync/atomic/" rel="nofollow noreferrer">atomic documentation</a> does say it should only be used in ...
  • <p>When programming in Node.js and referencing files that are located somewhere in relation to your current directory, is there any reason to use the <code>__dirname</code> variable instead of just a ...
  • I am from a Java background and new to Scala.I am using Scala and Spark. But I'm not able to understand where I use ==and ===.Could anyone let me know in which scenario I need to use these two operato...
  • #include#include的区别

    2013-06-24 08:38:17
    区别在于:如果用了,则一定要引入命名空间,即"using namespace std;如果用了,则不能引入命名空间,否则会引起编译错误,提示找不到命名空间. 例程如下:   //情况一:使用命名空间   #include   ...
  • <p>I have directed to use following scripts to do chat application for long pulling but in this script I am getting same effect using <code>usleep(25000);</code> or without <code>usleep(25000);</code>...
  • I have also seen <code>src="static/myscript.js"</code> and I don't know if there is a reason for using one or the other (but I guess it influences the handler we have to write on the server). ...

空空如也

空空如也

1 2 3 4 5
收藏数 91
精华内容 36
关键字:

using和use的区别