template constructor

This is a discussion on template constructor within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by ISO/IEC 14882:2003(E) Footnote of 7.3.1.1.2 82) Although entities in an unnamed namespace might have external linkage, they ...

  1. #16
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by ISO/IEC 14882:2003(E) Footnote of 7.3.1.1.2
    82) Although entities in an unnamed namespace might have external linkage, they are effectively qualified by a name unique to their translation unit and therefore can never be seen from any other translation unit.
    Quote Originally Posted by ISO/IEC 14882:2003(E) 2.1
    The text of the program is kept in units called source files in this International Standard. A source file together with all the headers (17.4.1.2) and source files included (16.2) via the preprocessing directive #include, less any source lines skipped by any of the conditional inclusion (16.1) preprocessing directives, is called a translation unit. [Note: a C + + program need not all be translated at the same time. ]
    Gotta love that C++ standard

  2. #17
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Lets see here:
    Quote Originally Posted by 14.1.1
    template-parameter:
    type-parameter
    parameter-declaration
    type-parameter:
    class identifieropt
    class identifieropt = type-id
    typename identifieropt
    typename identifieropt = type-id
    template < template-parameter-list > class identifieropt
    template < template-parameter-list > class identifieropt = id-expression
    Quote Originally Posted by 14.1.3
    A type-parameter defines its identifier to be a type-name (if declared with class or typename) or
    template-name (if declared with template) in the scope of the template declaration. [Note: because of
    the name lookup rules, a template-parameter that could be interpreted as either a non-type templateparameter
    or a type-parameter (because its identifier is the name of an already existing class) is taken as a
    type-parameter.
    Quote Originally Posted by 14.1.4
    A non-type template-parameter shall have one of the following (optionally cv-qualified) types:
    — integral or enumeration type,
    — pointer to object or pointer to function,
    — reference to object or reference to function,
    — pointer to member.
    Quote Originally Posted by 14.1.5-14.1.7
    5 [Note: other types are disallowed either explicitly below or implicitly by the rules governing the form of
    template-arguments (14.3). ] The top-level cv-qualifiers on the template-parameter are ignored when
    determining its type.
    6 A non-type non-reference template-parameter is not an lvalue. It shall not be assigned to or in any other
    way have its value changed. A non-type non-reference template-parameter cannot have its address taken.
    When a non-type non-reference template-parameter is used as an initializer for a reference, a temporary is
    always used.
    Theres more but I really don't feel like quoting the entire 14.1 section

  3. #18
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    82) Although entities in an unnamed namespace might have external linkage
    so
    Code:
    namespace{
        char buffer[256];
    }
    the buffer has external linkage, because no static specified

    so
    X<buffer> is correct.
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  4. #19
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Its not really external linkage as no one outside of the translation unit can get to that variable.

    Try this:
    file1.c
    Code:
    namespace {
      char buffer[256];
    };
    file2.c
    Code:
    extern buffer;
    X<buffer>
    And tell me if that works.

  5. #20
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    an unnamed namespace is the new way of saying the deprecated static. Anything in an unnamed namespace is only available to that translation unit in which the namespace is declared therefore linkage is internal.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #21
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    to Thantos:
    take a look at this code
    Code:
    //file.cpp
    namespace {
      char buffer_external[256];       //external linkage
      static char buffer_internal[256];  //internal linkage
    };
    
    template<char* P> class X{};
    
    int main()
    {
      X<buffer_external> x;   //it's OK, 
      X<buffer_internal> y;    //error, buffer_internal has not external linkage
    }
    back to your code
    Code:
    //file1.c
    namespace {
      char buffer[256];
    };
    
    //file2.c
    extern buffer;
    X<buffer> x;
    now, definition of x is failed, because buffer is in an unnamed namespace and in onther translation unit. but it does not indicate buffer has internal linkage, it still has external linkage, it just says buffer is not available to that translation

    refer to
    Although entities in an unnamed namespace might have external linkage, they are effectively qualified by a name unique to their translation unit and therefore can never be seen from any other translation unit.
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  7. #22
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Since you won't listen to me:
    Quote Originally Posted by 3.5.1
    2 A name is said to have linkage when it might denote the same object, reference, function, type, template, namespace or value as a name introduced by a declaration in another scope:
    — When a name has external linkage, the entity it denotes can be referred to by names from scopes of other translation units or from other scopes of the same translation unit.
    — When a name has internal linkage, the entity it denotes can be referred to by names from other scopes in the same translation unit.
    — When a name has no linkage, the entity it denotes cannot be referred to by names from other scopes.
    So if you can't name it in another translational unit then its not external linkage. But don't take my word for it:

    Quote Originally Posted by 3.5.4
    4 A name having namespace scope has external linkage if it is the name of
    — an object or reference, unless it has internal linkage; or
    — a function, unless it has internal linkage; or
    — a named class (clause 9), or an unnamed class defined in a typedef declaration in which the class has the typedef name for linkage purposes (7.1.3); or
    — a named enumeration (7.2), or an unnamed enumeration defined in a typedef declaration in which the enumeration has the typedef name for linkage purposes (7.1.3); or
    — an enumerator belonging to an enumeration with external linkage; or
    — a template, unless it is a function template that has internal linkage (clause 14); or
    — a namespace (7.3), unless it is declared within an unnamed namespace.
    Note: All bolding mine

  8. #23
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    refer to
    Originally Posted by 14.3.2
    A template-argument for a non-type, non-template template-parameter shall be one of;
    - an integral constant-expression of integral or enumeration type; or
    - the name of a non-type template-parameter; or
    - the name of an object or function with external linkage, including function templates and function template-ids but excluding non-static class members. expressed as & id-expression where the & is optional if the name refers to a function or array; or
    - a pointer to member expressed as described in 5.3.1
    what about this code
    Code:
    namespace {
      char buffer_external[256];       //external linkage
      static char buffer_internal[256];  //internal linkage
    };
    
    template<char* P> class X{};
    
    int main()
    {
      X<buffer_external> x;   //it's OK, 
      X<buffer_internal> y;    //error, buffer_internal has not external linkage
    }
    if buffer_external has internal linkage, how can be X<buffer_external> legal ?

    an unnamed namespace restricts the visibility of its members to the scope of the translation unit by means of name mangling, it doesn't necessarily guarantee internal linkage
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  9. #24
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I'm done with this. Anyone else want to try?

  10. #25
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    nah not worth the energy.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  3. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 09:42 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21