C ++模板参数和部分专业化:强类型还是弱类型? [英] C++ template parameter and partial specialization : strong or weak typing?

查看:108
本文介绍了C ++模板参数和部分专业化:强类型还是弱类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我的一个朋友和我为一个愚蠢的错误而苦苦挣扎,我让我想知道模板参数在C ++中是如何工作的。考虑下面的代码,在这里我尝试部分专门化类 attr< MyClass< I>> ,其中 I 是一个 unsigned int ,尽管 MyClass 期望一个 int 参数:

Today, a friend of mine and I struggled a lot on a stupid mistake, and I make me wondered about how template parameters work in C++. Consider the following code, where I try to partially specialize a class attr<MyClass<I>> where I is an unsigned int, though MyClass expects an int parameter :

#include <iostream>

template<int I>
class MyClass
{

};

template<typename T>
struct attr;

template<unsigned int I>
struct attr<MyClass<I>>
{

};

int main(int argc, char *argv[])
{
    attr<MyClass<1>> att;
    return 0;
}

g ++ 失败,错误消息

main.cpp: In function ‘int main(int, char**)’:
main.cpp:20:22: erreur : aggregate ‘attr<MyClass<1> > att’ has incomplete type and cannot be defined
     attr<MyClass<1>> att;

然后 clang 对其进行编译(仅警告由于未使用 att )。

And clang compiles it (only a warning due to the fact that att is unused).

所以我想知道:


  • 规范中是否有任何规则可以支持一个或另一个?

  • is there anything in the spec that would rule in favor of one or the other ?

我们可以说 clang 模板参数的键入要比 g ++ 的弱吗?

could we say that clang template parameter's typing is weaker than g++'s ?

推荐答案

是的,至少根据现行标准,GCC拒绝是正确的。也许C族人会在这里实施一些缺陷报告,我不知道。

Yes, GCC is correct to reject, at least according to current Standards. Perhaps Clang folks implement some defect report here, I wouldn't know.

http://eel.is/c++draft/temp.deduct.type#17


如果P的格式包含< i> ,并且如果A对应值的类型不同于i的类型,推论失败。如果P的形式包含 [i] ,并且如果i的类型不是整数类型,则推导将失败。

If P has a form that contains <i>, and if the type of the corresponding value of A differs from the type of i, deduction fails. If P has a form that contains [i], and if the type of i is not an integral type, deduction fails.

他们在测试套件中的测试用例仅针对功能进行测试,对于它们看来会发出明显的错误消息: https://github.com/llvm-mirror /clang/blob/master/test/CXX/temp/temp.fct.spec/temp.deduct/temp.deduct.type/p17.cpp

Their testcase in their testsuite tests this only for for functions, for which they appear to emit sensible error messages: https://github.com/llvm-mirror/clang/blob/master/test/CXX/temp/temp.fct.spec/temp.deduct/temp.deduct.type/p17.cpp.

此外,由于无法推断出部分专业化,因此我们也遇到了 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#549 ,询问是否这样的构造可能应该被预先拒绝。我认为 http://eel.is/c++draft/temp.res #8 可以根据需要应用:

Also, since the partial specialization can never be deduced, we also run into http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#549 , which asks whether such constructs should possibly be rejected up-front. In my opinion, http://eel.is/c++draft/temp.res#8 could be applied if you wanted:


知道哪些名称是类型名称,则可以将每个模板的语法该程序格式错误,无需进行诊断,如果:

"Knowing which names are type names allows the syntax of every template to be checked. The program is ill-formed, no diagnostic required, if:


  • 无法为模板生成有效的专业化名称,并且该模板为没有实例化,或者...

没有合法的方法来触发该实例化模板,因此您可能会争辩说无法为其生成有效的专业化名称。根据这种解释,行为是不确定的,任何事情都可以做。

There is no legal way to trigger an instantiation of that template, hence you could argue that no valid specialization can be generated for it. Under that interpretation, behavior is undefined and anything is legal to do.

这篇关于C ++模板参数和部分专业化:强类型还是弱类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆