问题标签 [variable-templates]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
120 浏览

c++ - 存储在变量模板特化中的 Spirit-X3 解析器不适用于 Clang

我有一个可以工作的 Spirit-X3 解析器,它可以解析两个密切相关的语法来设置草稿和检查器的位置。我定义了两个变量模板特化作为语法的两种方言的解析器:

此代码编译并正确解析我在 Clangg++上的测试输入。

由于两个变量模板特化依赖于完全相同形式的模板参数,我想将它们合并到一个通用变量模板中:

这也为g++正确编译和解析。它也为 Clang 编译,但它只能正确解析我在Wandbox上的输入,而不是在Coliru上。在我自己的开发盒上,使用来自apt.llvm.org的 clang-3.8.0 ,我得到了与 Coliru 相同的错误行为。

问题:Clang 中是否存在变量模板专业化的错误?如何以与 Wandbox 相同的方式配置 Clang 以解决该错误?或者它是某种与 Spirit-X3 相关的错误?

0 投票
2 回答
306 浏览

c++ - 是否可以通过 lambda 将变量模板传递给函数?

正如我知道如何将模板函数作为模板参数传递一样,我现在正努力以类似的方式传递变量模板。

这是我尝试过的最小示例:

演示

如果它编译,那么为什么输出是错误的?

0 投票
2 回答
527 浏览

c++ - 可变模板模板?

假设您有一个元组类型,并且您想要提取其模板参数包以实例化另一个模板。如果那是一个类型模板,那么我可以有一个这样的实用程序:

但是,如果所需的模板是可变模板怎么办?虽然template <typename...> typename What是类型模板的“占位符”,但变量模板的“占位符”是什么?

我已经为 clang-4.0.0(目前唯一支持具有自动类型的非类型模板参数的编译器)尝试了以下操作,但它失败了。实际上我不确定这是否是 C++17 的正确语法。

0 投票
0 回答
89 浏览

c++ - 获取静态成员变量模板的多个定义的链接器错误

当我有一个可以归结为这样的课程时:

value当我只在一个 .cpp 文件中查找实例时,我可以使用这个类。但是当我尝试在多个 .cpp 文件中使用它时,我得到一个已经定义的链接器错误。

放入template<typename T> Test::value = {}其自己的 .cpp 文件会为所有用途提供未解决的外部错误。有什么问题?

0 投票
0 回答
89 浏览

openmp - OpenMP 线程私有变量模板

我也想要一个变量模板threadprivate。这可以做到吗?类似于以下内容:

0 投票
1 回答
937 浏览

c++ - How should a Variable Template be referred to in C++14 when declared at Class scope?

For example:

GCC produces error for both: 'example::var<T>' is not a function template and 'var' is not a member template function

Clang compiles correctly the first one but produces an error for the second: cannot refer to member 'var' in 'example' with '.'

According to the C++14 Standard (ISO/IEC 14882:2014):

Section 14, Paragraph 1.

A variable template at class scope is a static data member template.

Section 9.4, Paragraph 2.

A static member s of class X may be referred to using the qualified-id expression X::s; it is not necessary to use the class member access syntax (5.2.5) to refer to a static member. A static member may be referred to using the class member access syntax, in which case the object expression is evaluated.

Therefore, IMHO, a Variable Template at Class scope (i.e., a Static Data Member Template) could be referred to in both ways. Could it be a bug in the compilers?

The only thing I found to try to justify this behaviour is this sentence in Section 9.4.2, Paragraph 1:

A static data member is not part of the subobjects of a class.

However, the two aforementioned paragraphs are still valid. Besides, I tried the same example referring to other static members such as a variable, a function and a function template, and all of them compile successfully in both GCC and Clang.

Thanks in Advance.

Note 1: compiler versions are clang 3.7.0 and gcc 5.2.1.

Note 2: the keyword static is required: Variable template at class scope

Note 3: since I want to initialize the variable template, the keyword constexpr is also required because in my actual code I will instantiate it with float, double and long double (see C++14 Standard (ISO/IEC 14882:2014), Section 9.4.2, Paragraph 3).

Note 4: actual "definitions" of these static data members outside the class (i.e., template <class T> constexpr T example::var;) are not needed in this example cases. I also tried though, but it makes no difference.

0 投票
1 回答
65 浏览

c++ - 变量模板可以变异吗?

今天,有人向我展示了以下形式的代码:

你可以在这里看到它运行:http: //coliru.stacked-crooked.com/a/3a786c42b5204b0a

我预计此代码将打印 42,因为 10 似乎分配给了一个临时的。在命名空间内,模板只有一个声明(不是实例化),因此在命名空间内没有数据可以改变。尽管如此,它还是让我感到惊讶并打印了 10 个。

我本来也希望对临时分配的警告发出警告,但这也没有发生。

这是未定义的行为,是我对模板的理解有缺陷,还是发生了其他事情?

0 投票
2 回答
582 浏览

c++ - 在内联 constexpr 函数中使用变量模板而不暴露变量模板?

是否可以在内联 constexpr 函数中使用变量模板而不暴露变量模板本身?

例如,这编译并工作:

但这不会编译:

原因似乎是块范围内不允许模板声明(GCC 提供了一个关于此的信息性错误消息,Clang 没有)。

为了更详细地重复动机,该函数是内联的并在标题中定义,并且我对在包含标题的任何地方公开变量模板不感兴趣。

我想我可以定义一个详细的命名空间并将变量模板放在那里,但是完全不公开变量模板会更好。也许这是不可能的。

0 投票
0 回答
320 浏览

c++ - Visual Studio 没有在 type_traits 中定义模板变量

当我尝试使用std::extent_v模板时,它带有红色下划线,并且在 IDE 中出现错误“命名空间 std 不包含成员 extent_v”,但它编译时没有错误(<type_traits>当然包括在内)。

当我发现它的定义<type_traits>与其他变量模板定义一起被禁用时 conditional #if _HAS_VARIABLE_TEMPLATES. _HAS_VARIABLE_TEMPLATES根据__EDG__宏定义为 0,源代码中没有引入。

这一切意味着什么,我该如何修复 IDE?

0 投票
1 回答
494 浏览

c++ - C ++使变量的类型取决于上下文?

我有以下代码:

有什么方法可以定义变量pi,以便在案例 #1 中对 s 进行操作,但在operator*案例#2 中对 s 进行操作?sqrtfloatdouble

也许使用 C++14 变量模板是可能的?