英文:
Is There any Way to associate a Value to a Type?
问题
我已经翻译好了您提供的代码部分:
所以我已经编写了一个基本的类型标记结构:
struct TypeTag
{
inline static size_t counter = 0;
template<typename T>
static size_t get()
{
static size_t value = counter++;
return value;
}
};
class c1 {};
class c2 {};
class c3 {};
class c4 {};
int main()
{
std::cout << "c1: " << TypeTag::get<c1>() << '\n';
std::cout << "c2: " << TypeTag::get<c2>() << '\n';
std::cout << "c3: " << TypeTag::get<c3>() << '\n';
std::cout << "c4: " << TypeTag::get<c4>() << '\n';
std::cout << "\n================================\n\n";
std::cout << "c1: " << TypeTag::get<c1>() << '\n';
std::cout << "c2: " << TypeTag::get<c2>() << '\n';
std::cout << "c3: " << TypeTag::get<c3>() << '\n';
std::cout << "c4: " << TypeTag::get<c4>() << '\n';
return 0;
}
输出:
c1: 0
c2: 1
c3: 2
c4: 3
================================
c1: 0
c2: 1
c3: 2
c4: 3
这将把类型与值关联起来。
这是它的一个工作示例:https://godbolt.org/z/9rqK6Pasz
假设我们在编译时知道值:template <size_t value>
;是否有办法做相反的事情?是否有办法将变量值与类型关联起来?
template <size_t value>
struct TypeFrom
{
using type = ... // 这里是 SOMETHING HERE
};
英文:
So I've written a basic type tagging structure:
struct TypeTag
{
inline static size_t counter = 0;
template<typename T>
static size_t get()
{
static size_t value = counter++;
return value;
}
};
class c1 {};
class c2 {};
class c3 {};
class c4 {};
int main()
{
std::cout << "c1: " << TypeTag::get<c1>() << '\n';
std::cout << "c2: " << TypeTag::get<c2>() << '\n';
std::cout << "c3: " << TypeTag::get<c3>() << '\n';
std::cout << "c4: " << TypeTag::get<c4>() << '\n';
std::cout << "\n================================\n\n";
std::cout << "c1: " << TypeTag::get<c1>() << '\n';
std::cout << "c2: " << TypeTag::get<c2>() << '\n';
std::cout << "c3: " << TypeTag::get<c3>() << '\n';
std::cout << "c4: " << TypeTag::get<c4>() << '\n';
return 0;
}
Output:
c1: 0
c2: 1
c3: 2
c4: 3
================================
c1: 0
c2: 1
c3: 2
c4: 3
This will associate a type to a value.
Here's a working example of it: https://godbolt.org/z/9rqK6Pasz
Suppose we know the value at compile time: template <size_t value>
; is there any way to do the opposite? Is there any way to associate a variable value to a type?
template <size_t value>
struct TypeFrom
{
using type = ... // SOMETHING HERE
};
答案1
得分: 2
如图所示,由于某些相当基本的原因,这是不可能的:C++ 简单地不以这种方式工作。
TypeTag::get<T>
的值仅在运行时知道。对于给定的 T
,其值取决于首次调用此特定 T
的模板实例相对于其他类型的首次调用的顺序。每次程序运行时,相同的类型可能会获得不同的 TypeTag
是完全可能的。这对于 C++ 来说是基本的:static
范围对象在其范围首次进入时构造。模板生成的范围这一事实无关紧要。事实上,counter++
在首次调用其 get()
时被计算以初始化 static size_t value
。
因此,说到底,特定的 TypeTag::get<T>
值仅在运行时知道。
但模板参数(如 ValueType<n>
中的 size_t n
)必须在编译时知道。这是 C++ 的基本原则。结束。
英文:
As shown, this is not possible for some fairly fundamental reasons: C++ simply does not work this way.
The value of TypeTag::get<T>
is known only at runtime. The value for a given T
depends on the order of the first call to a template instance for this particular T
relative to first calls for other types. It's entirely possible that each time the program runs the same types get different TypeTag
s. This is fundamental to C++: a static
ally-scoped object is constructed the first time its scope gets entered. The fact that the scope is generated from a template is immaterial. Effectively counter++
gets evaluated, to initialize the static size_t value
the first time its get()
gets called.
So, when all is said and done, a particular TypeTag::get<T>
value is known only at runtime.
But templates parameters (such as the size_t n
in ValueType<n>
) must be known at compile time. This is fundamental to C++. The End.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论