#include <iostream>
#include <atomic>
using namespace std;
template<typename T>
atomic<T> JustForFun{};
// need symbol from other TU.
extern template atomic<int> JustForFun<int>;
//same as below:
template<>
extern atomic<char> JustForFun<char>;
template<typename T>
struct FunType
{
auto& GiveMeAtomic()
{
return JustForFun<T>;
}
};
int main()
{
JustForFun<char> = 'V';
cout << JustForFun<char> << endl;
FunType<long long> ft;
cout << ft.GiveMeAtomic() << endl;
FunType<int> ft_int;
cout <<< ft_int.GiveMeAtomic() << endl;
ft_int.GiveMeAtomic() = 24;
cout << ft_int.GiveMeAtomic() << endl;
}
Library.o
#include <atomic>
using namespace std;
template<typename T>
atomic<T> JustForFun{};
template <typename T>
constexpr T genT()
{
return T{42};
}
//specialize variable template, will generate/export symbol
template<>
atomic<int> JustForFun<int>{genT<int>()};
// explicit generate variable template symbol for type char.
// Variable template could be seen as template class with static data member.
template atomic<char> JustForFun<char>;
Reference:
Variable templates and instantiation -- Is this well formed?
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.