llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Global templated variable is not defined

Open Oscardoras opened this issue 2 years ago • 2 comments

Tested Clang Version: 17.0.3 Platform: Windows C++ version: 20

Clang for Windows will create an executable that crashes while executing the last line of this code :

#include <iostream>
#include <typeindex>


template<typename T>
const std::type_index type = typeid(T);

const std::type_index type_int_hard = typeid(int);

const std::type_index type_int_templated = type<int>;

int main(int, char**) {
    std::cout << "Local version: " << type<int>.name() << std::endl;
    std::cout << "Global hard version: " << type_int_hard.name() << std::endl;
    std::cout << "Global templated version: " << type_int_templated.name() << std::endl;
}

In fact the variable type_int_templated has zero binary data.

It seems that it is not possible to use a templated global variable in the definition of another global variable.

However this code runs perfectly with Mingw for Windows and also GCC and Clang for Linux. Moreover this code appears to be in agreement with this article: https://en.cppreference.com/w/cpp/language/initialization

Oscardoras avatar Apr 13 '24 08:04 Oscardoras

Could you please try 18 or main branch?

EugeneZelenko avatar Apr 13 '24 14:04 EugeneZelenko

This might be an MSVC STL issue. Reproduced on clang 18 (with MSVC STL)

clang version 18.1.2 (https://github.com/llvm/llvm-project.git 26a1d6601d727a96f4301d0d8647b5a42760ae0c)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin

and MSVC 19.39.

MagentaTreehouse avatar Apr 13 '24 20:04 MagentaTreehouse