Does the static object in the template function have a link?

This question relates to my previous question: clang does not compile my code, but g ++ does . From my research, the problem in question comes down to binding, does the static data variable have a connection in the example below (it compiles with g++-4.8.1 )? Why is this related to binding (otherwise I would not be able to create an instance with a non-type template parameter)?

 template <int const* ptr> void foo() { } typedef void (*func_type)(); template <int = 0> void run_me() { static int data; func_type const f1 = foo<&data>; // auto f2 = foo<&data>; // doesn't work with gcc // foo<&data>(); // doesn't work with gcc } int main(int, char*[]) { run_me(); return 0; } 

Mandatory quote from the standard:

The template argument for a non-piggy template without a template must be one of the following: ...

- constant expression (5.19), which denotes the address of an object with a static storage time and external or internal communication or a function with external or internal communication, including functional templates and function template functions, but excluding non-static class members expressed (ignoring parentheses) as & Amp; id-expression, except that it can be omitted if the name refers to a function or array and should be omitted if the corresponding parameter template is a link; or ...

0
source share
1 answer

Of course, a static variable declared in a function (regardless of whether it is a template function) has no binding.

& sect; 3.5 para. 8: "except as indicated, the name declared in the block area (3.3.3) has no relationship"

The only exceptions listed in this paragraph, as far as I see, are given in the paragraph. 6: "The name of the function declared in the block scope and the name of the variable declared by the extern declaration of the block have a binding."

However, it is possible that 14.3.2 may be weakened at some point.

Daniel Krugler introduced DR 1451 on 2012-02-01:

According to 14.3.2 [temp.arg.nontype] of paragraph 1 of paragraph 3, only objects with a binding can be used to form arguments of a non-type type template. Is this restriction still necessary? It would be convenient to use block area objects as arguments to the template.

The DR was closed on the grounds that it was an extension request and should be decided by the Evolution Working Group. It seems to have been included in n3413 , "Allow arbitrary literals for non-piggy type template parameters."

Thus, of course, it is possible that one or more C ++ compilers can choose a restriction on fuzzy template parameters.

+3
source

Source: https://habr.com/ru/post/948280/


All Articles