Non-type template parameter restrictions--internal linked objects cannot be used

Source: Internet
Author: User
Tags modifier

The majority of this article is excerpted from a http://www.csdn.net question. I am not very familiar with the limitations of template parameters, but in the course of learning that these comments and questions for my study is very helpful, excerpts here. If you have different opinions, please leave a message and discuss together. Thanks

It is a puzzling question to see "C++templates Full Guide" recently.
In the book p.40, p.110

Template <char const* name>
Class MyClass {
...
};
Char const* s = "Hello";
Myclass<s> x; Error:s is pointer to object with internal linkage
Here "Hello" is an internal link (internal linkage) object
But:
Template <char const* name>
Class MyClass {
...
};
extern char const s[] = "Hello";
Myclass<s> x; Ok
(1) "Hello" is a string constant, because it is not "variable", so there is no internal, external link property.
The one with the internal external link attribute is that s.
(2) C + + regulation, there are const modified variables, not only can not be modified, but also will have the internal link properties, that is, only in this document is visible. (This is the original C language static modifier function, now the const also has this function.) added that the extern const union modifier, extern will suppress const this internal link property. As a result, extern char cosnt s[] will still have an external link property, but it is still not modifiable.

(3) 1.char const* s = "Hello";
Myclass<s> x; Error:s is pointer to object with internal linkage

I do not know how the LZ callout error information is obtained, under the VS2005 error message is:
Invalid template argument for ' MyClass ', expected Compile-time constant, expression
The main reason is that template is generated at compile time, and S is a pointer variable whose value is Run-time knowable.

(4) This passage is the original author of the sentiment, I am not very understanding, but also a temporary excerpt from this.

Nontype template parameters requirements are required to be knowable at compile or link values. For internal linked objects, it is not visible to other compilation units, and at least the instantiation of the template at compile time is shared (Blue_zyb said I agree), but if an internal linked object can be a semantic error as a template argument (template argument), For example, string literals (such as "ABCX", which I said are string constants, are inaccurate). What he passed to the template was a pointer, rather than his value ("Abcx"). Moreover, if there is a "abcx", it is also the address, and the two addresses are theoretically different (even if some compilers will do optimizations to make them the same). But he's not the same for our users. But at the same time we have to say that the values of the two string literal quantities are the same. You should share the definition of a class (note: This is our user's point of view), and the compiler cannot actually pass the constant as a value, but pass the constant in the form of an address. So if the compiler generates a different class implementation, it violates the information that the template gave me (the same value of the non-type "nontype template parameters" template parameter generates an object that is a class that shares an instance), so the compiler should not generate an implementation of the corresponding class.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.