有没有办法将一个值关联到一个类型?

huangapple go评论80阅读模式
英文:

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&lt;typename T&gt;
    static size_t get()
    {
        static size_t value = counter++;
        return value;
    }
};

class c1 {};
class c2 {};
class c3 {};
class c4 {};

int main()
{
    std::cout &lt;&lt; &quot;c1: &quot; &lt;&lt; TypeTag::get&lt;c1&gt;() &lt;&lt; &#39;\n&#39;;
    std::cout &lt;&lt; &quot;c2: &quot; &lt;&lt; TypeTag::get&lt;c2&gt;() &lt;&lt; &#39;\n&#39;;
    std::cout &lt;&lt; &quot;c3: &quot; &lt;&lt; TypeTag::get&lt;c3&gt;() &lt;&lt; &#39;\n&#39;;
    std::cout &lt;&lt; &quot;c4: &quot; &lt;&lt; TypeTag::get&lt;c4&gt;() &lt;&lt; &#39;\n&#39;;

    std::cout &lt;&lt; &quot;\n================================\n\n&quot;;

    std::cout &lt;&lt; &quot;c1: &quot; &lt;&lt; TypeTag::get&lt;c1&gt;() &lt;&lt; &#39;\n&#39;;
    std::cout &lt;&lt; &quot;c2: &quot; &lt;&lt; TypeTag::get&lt;c2&gt;() &lt;&lt; &#39;\n&#39;;
    std::cout &lt;&lt; &quot;c3: &quot; &lt;&lt; TypeTag::get&lt;c3&gt;() &lt;&lt; &#39;\n&#39;;
    std::cout &lt;&lt; &quot;c4: &quot; &lt;&lt; TypeTag::get&lt;c4&gt;() &lt;&lt; &#39;\n&#39;;
    
    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 &lt;size_t value&gt;; is there any way to do the opposite? Is there any way to associate a variable value to a type?

template &lt;size_t value&gt;
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&lt;T&gt; 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 TypeTags. This is fundamental to C++: a statically-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&lt;T&gt; value is known only at runtime.

But templates parameters (such as the size_t n in ValueType&lt;n&gt;) must be known at compile time. This is fundamental to C++. The End.

huangapple
  • 本文由 发表于 2023年7月14日 07:58:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683934.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定