Clang fails to initialize static const template member











up vote
1
down vote

favorite












In order to force the execution of a template method at program start one can initialize a static member with a static method. Then the method is run at program start for every instantiation of the template class:



#include <cstdio>


template<typename t, t value>
struct dummy_user_t {};

template<int i>
struct my_struct_t
{
static int s_value;

// "use" s_value so it's initialized
using value_user_t = dummy_user_t<const int&, s_value>;
static int method()
{
printf("Hello %i!n", i);
return 0;
}
};

// initialize s_value with method() to run it at program start
template<int i>
int my_struct_t<i>::s_value {my_struct_t<i>::method()};

// instantiate my_struct_t
template struct my_struct_t<6>;

int main()
{
// nothing here
}


The output will be Hello 6!



This code compiles on all three major compilers but when you make s_value const it won't work in clang anymore (3.4 - 7.0) while still working in MSVC and GCC:



<source>:19:52: error: no member 'method' in 'my_struct_t<6>'; it has not yet been instantiated

const int my_struct_t<i>::s_value {my_struct_t<i>::method()};

^

<source>:10:51: note: in instantiation of static data member 'my_struct_t<6>::s_value' requested here

using value_user_t = dummy_user_t<const int&, s_value>;

^

<source>:21:17: note: in instantiation of template class 'my_struct_t<6>' requested here

template struct my_struct_t<6>;

^

<source>:11:16: note: not-yet-instantiated member is declared here

static int method()

^

1 error generated.


Try it out yourself:



With non const int: https://godbolt.org/z/m90bgS



With const int: https://godbolt.org/z/D3ywDq



What do you think? Is there any reason clang is rejecting this or is it a bug?










share|improve this question






















  • Move the explicit instantiation above the usage of my_struct_t<i>::method() and clang compiles like a champ: godbolt.org/z/758iFF
    – Swordfish
    Nov 15 at 18:30










  • @Swordfish while that's true it defeats the purpose of being able to create new instantiation from anywhere in the code.
    – HenrikS
    Nov 15 at 18:33










  • Another fix would be to move the type alias declaration beneath the definition of my_struct_t<i>::method().
    – Swordfish
    Nov 15 at 19:35












  • Well, making s_value mutable isn't a big deal for my project. I was just curious which compiler is wrong.
    – HenrikS
    Nov 15 at 19:37










  • Please re-read my last comment. You can have your int const with clang if you move using value_user_t = dummy_user_t<const int&, s_value>; to after the definition of method().
    – Swordfish
    Nov 15 at 19:38















up vote
1
down vote

favorite












In order to force the execution of a template method at program start one can initialize a static member with a static method. Then the method is run at program start for every instantiation of the template class:



#include <cstdio>


template<typename t, t value>
struct dummy_user_t {};

template<int i>
struct my_struct_t
{
static int s_value;

// "use" s_value so it's initialized
using value_user_t = dummy_user_t<const int&, s_value>;
static int method()
{
printf("Hello %i!n", i);
return 0;
}
};

// initialize s_value with method() to run it at program start
template<int i>
int my_struct_t<i>::s_value {my_struct_t<i>::method()};

// instantiate my_struct_t
template struct my_struct_t<6>;

int main()
{
// nothing here
}


The output will be Hello 6!



This code compiles on all three major compilers but when you make s_value const it won't work in clang anymore (3.4 - 7.0) while still working in MSVC and GCC:



<source>:19:52: error: no member 'method' in 'my_struct_t<6>'; it has not yet been instantiated

const int my_struct_t<i>::s_value {my_struct_t<i>::method()};

^

<source>:10:51: note: in instantiation of static data member 'my_struct_t<6>::s_value' requested here

using value_user_t = dummy_user_t<const int&, s_value>;

^

<source>:21:17: note: in instantiation of template class 'my_struct_t<6>' requested here

template struct my_struct_t<6>;

^

<source>:11:16: note: not-yet-instantiated member is declared here

static int method()

^

1 error generated.


Try it out yourself:



With non const int: https://godbolt.org/z/m90bgS



With const int: https://godbolt.org/z/D3ywDq



What do you think? Is there any reason clang is rejecting this or is it a bug?










share|improve this question






















  • Move the explicit instantiation above the usage of my_struct_t<i>::method() and clang compiles like a champ: godbolt.org/z/758iFF
    – Swordfish
    Nov 15 at 18:30










  • @Swordfish while that's true it defeats the purpose of being able to create new instantiation from anywhere in the code.
    – HenrikS
    Nov 15 at 18:33










  • Another fix would be to move the type alias declaration beneath the definition of my_struct_t<i>::method().
    – Swordfish
    Nov 15 at 19:35












  • Well, making s_value mutable isn't a big deal for my project. I was just curious which compiler is wrong.
    – HenrikS
    Nov 15 at 19:37










  • Please re-read my last comment. You can have your int const with clang if you move using value_user_t = dummy_user_t<const int&, s_value>; to after the definition of method().
    – Swordfish
    Nov 15 at 19:38













up vote
1
down vote

favorite









up vote
1
down vote

favorite











In order to force the execution of a template method at program start one can initialize a static member with a static method. Then the method is run at program start for every instantiation of the template class:



#include <cstdio>


template<typename t, t value>
struct dummy_user_t {};

template<int i>
struct my_struct_t
{
static int s_value;

// "use" s_value so it's initialized
using value_user_t = dummy_user_t<const int&, s_value>;
static int method()
{
printf("Hello %i!n", i);
return 0;
}
};

// initialize s_value with method() to run it at program start
template<int i>
int my_struct_t<i>::s_value {my_struct_t<i>::method()};

// instantiate my_struct_t
template struct my_struct_t<6>;

int main()
{
// nothing here
}


The output will be Hello 6!



This code compiles on all three major compilers but when you make s_value const it won't work in clang anymore (3.4 - 7.0) while still working in MSVC and GCC:



<source>:19:52: error: no member 'method' in 'my_struct_t<6>'; it has not yet been instantiated

const int my_struct_t<i>::s_value {my_struct_t<i>::method()};

^

<source>:10:51: note: in instantiation of static data member 'my_struct_t<6>::s_value' requested here

using value_user_t = dummy_user_t<const int&, s_value>;

^

<source>:21:17: note: in instantiation of template class 'my_struct_t<6>' requested here

template struct my_struct_t<6>;

^

<source>:11:16: note: not-yet-instantiated member is declared here

static int method()

^

1 error generated.


Try it out yourself:



With non const int: https://godbolt.org/z/m90bgS



With const int: https://godbolt.org/z/D3ywDq



What do you think? Is there any reason clang is rejecting this or is it a bug?










share|improve this question













In order to force the execution of a template method at program start one can initialize a static member with a static method. Then the method is run at program start for every instantiation of the template class:



#include <cstdio>


template<typename t, t value>
struct dummy_user_t {};

template<int i>
struct my_struct_t
{
static int s_value;

// "use" s_value so it's initialized
using value_user_t = dummy_user_t<const int&, s_value>;
static int method()
{
printf("Hello %i!n", i);
return 0;
}
};

// initialize s_value with method() to run it at program start
template<int i>
int my_struct_t<i>::s_value {my_struct_t<i>::method()};

// instantiate my_struct_t
template struct my_struct_t<6>;

int main()
{
// nothing here
}


The output will be Hello 6!



This code compiles on all three major compilers but when you make s_value const it won't work in clang anymore (3.4 - 7.0) while still working in MSVC and GCC:



<source>:19:52: error: no member 'method' in 'my_struct_t<6>'; it has not yet been instantiated

const int my_struct_t<i>::s_value {my_struct_t<i>::method()};

^

<source>:10:51: note: in instantiation of static data member 'my_struct_t<6>::s_value' requested here

using value_user_t = dummy_user_t<const int&, s_value>;

^

<source>:21:17: note: in instantiation of template class 'my_struct_t<6>' requested here

template struct my_struct_t<6>;

^

<source>:11:16: note: not-yet-instantiated member is declared here

static int method()

^

1 error generated.


Try it out yourself:



With non const int: https://godbolt.org/z/m90bgS



With const int: https://godbolt.org/z/D3ywDq



What do you think? Is there any reason clang is rejecting this or is it a bug?







c++ clang compiler-bug






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 at 18:25









HenrikS

148110




148110












  • Move the explicit instantiation above the usage of my_struct_t<i>::method() and clang compiles like a champ: godbolt.org/z/758iFF
    – Swordfish
    Nov 15 at 18:30










  • @Swordfish while that's true it defeats the purpose of being able to create new instantiation from anywhere in the code.
    – HenrikS
    Nov 15 at 18:33










  • Another fix would be to move the type alias declaration beneath the definition of my_struct_t<i>::method().
    – Swordfish
    Nov 15 at 19:35












  • Well, making s_value mutable isn't a big deal for my project. I was just curious which compiler is wrong.
    – HenrikS
    Nov 15 at 19:37










  • Please re-read my last comment. You can have your int const with clang if you move using value_user_t = dummy_user_t<const int&, s_value>; to after the definition of method().
    – Swordfish
    Nov 15 at 19:38


















  • Move the explicit instantiation above the usage of my_struct_t<i>::method() and clang compiles like a champ: godbolt.org/z/758iFF
    – Swordfish
    Nov 15 at 18:30










  • @Swordfish while that's true it defeats the purpose of being able to create new instantiation from anywhere in the code.
    – HenrikS
    Nov 15 at 18:33










  • Another fix would be to move the type alias declaration beneath the definition of my_struct_t<i>::method().
    – Swordfish
    Nov 15 at 19:35












  • Well, making s_value mutable isn't a big deal for my project. I was just curious which compiler is wrong.
    – HenrikS
    Nov 15 at 19:37










  • Please re-read my last comment. You can have your int const with clang if you move using value_user_t = dummy_user_t<const int&, s_value>; to after the definition of method().
    – Swordfish
    Nov 15 at 19:38
















Move the explicit instantiation above the usage of my_struct_t<i>::method() and clang compiles like a champ: godbolt.org/z/758iFF
– Swordfish
Nov 15 at 18:30




Move the explicit instantiation above the usage of my_struct_t<i>::method() and clang compiles like a champ: godbolt.org/z/758iFF
– Swordfish
Nov 15 at 18:30












@Swordfish while that's true it defeats the purpose of being able to create new instantiation from anywhere in the code.
– HenrikS
Nov 15 at 18:33




@Swordfish while that's true it defeats the purpose of being able to create new instantiation from anywhere in the code.
– HenrikS
Nov 15 at 18:33












Another fix would be to move the type alias declaration beneath the definition of my_struct_t<i>::method().
– Swordfish
Nov 15 at 19:35






Another fix would be to move the type alias declaration beneath the definition of my_struct_t<i>::method().
– Swordfish
Nov 15 at 19:35














Well, making s_value mutable isn't a big deal for my project. I was just curious which compiler is wrong.
– HenrikS
Nov 15 at 19:37




Well, making s_value mutable isn't a big deal for my project. I was just curious which compiler is wrong.
– HenrikS
Nov 15 at 19:37












Please re-read my last comment. You can have your int const with clang if you move using value_user_t = dummy_user_t<const int&, s_value>; to after the definition of method().
– Swordfish
Nov 15 at 19:38




Please re-read my last comment. You can have your int const with clang if you move using value_user_t = dummy_user_t<const int&, s_value>; to after the definition of method().
– Swordfish
Nov 15 at 19:38

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325739%2fclang-fails-to-initialize-static-const-template-member%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325739%2fclang-fails-to-initialize-static-const-template-member%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?