decltype 的另一个问题

发布于 2024-10-18 15:49:03 字数 679 浏览 6 评论 0原文

template<class IntT, IntT low = IntT(), IntT high = IntT()>
struct X 
{
    static_assert(std::is_same<decltype(low),decltype(high)>::value,"Different types not allowed");//this should give error if types are different
    decltype(low) a;
    decltype(high) b;
    X():a(decltype(a)()),b(decltype(b)())//WHY THIS DOES NOT COMPILE?
    {
        cout << typeid(a).name() << '\n';
        cout << typeid(b).name() << '\n';
    }
};

int _tmain(int argc, _TCHAR* argv[])
{


    X<char,1,'a'> x;//this according to static_assert shouldn't compile but it does

    return 0;
}

使用VS2010。
请参阅上面代码中的 3 条注释。

template<class IntT, IntT low = IntT(), IntT high = IntT()>
struct X 
{
    static_assert(std::is_same<decltype(low),decltype(high)>::value,"Different types not allowed");//this should give error if types are different
    decltype(low) a;
    decltype(high) b;
    X():a(decltype(a)()),b(decltype(b)())//WHY THIS DOES NOT COMPILE?
    {
        cout << typeid(a).name() << '\n';
        cout << typeid(b).name() << '\n';
    }
};

int _tmain(int argc, _TCHAR* argv[])
{


    X<char,1,'a'> x;//this according to static_assert shouldn't compile but it does

    return 0;
}

Using VS2010.
Please see 3 comments in code above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤千羽 2024-10-25 15:49:03

首先值得注意的是,VS2010 已经过时了,并且在发布之日就被破坏了。 decltype 关键字尤其有问题,并且仅适用于最基本的用途。事实上,它把很多基本的事情都搞错了。

接下来是代码......

template<class IntT, IntT low = IntT(), IntT high = IntT()>
struct X 
{
    static_assert(std::is_same<decltype(low),decltype(high)>::value,"Different types not allowed");//this should give error if types are different

但他们永远不会。

    decltype(low) a;
    decltype(high) b;

这里不需要 decltype。类型为 IntT。

    X():a(decltype(a)()),b(decltype(b)())//WHY THIS DOES NOT COMPILE?

因为 VS2010 已损坏,并且通常不允许您使用 decltype 表达式,就好像它是类型一样。事先 typedef 可能会更好。

幸运的是,您不需要这个,因为您可以只使用默认构造函数而不是副本。

int _tmain(int argc, _TCHAR* argv[])
{


    X<char,1,'a'> x;//this according to static_assert shouldn't compile but it does

否。 static_assert 检查类型是否相同。它们都是 1 和“a”的 char

    return 0;
}

您似乎正在尝试创建一个模板,以便第二个和第三个参数的类型基于您传递给它的值的解析类型。这是不可能的。

First thing of note, VS2010 is outdated and was broken the day it was released. The decltype keyword was especially problematic and only works for the most basic of uses. In fact it gets a lot of basic things quite wrong.

Next the code...

template<class IntT, IntT low = IntT(), IntT high = IntT()>
struct X 
{
    static_assert(std::is_same<decltype(low),decltype(high)>::value,"Different types not allowed");//this should give error if types are different

But they never will be.

    decltype(low) a;
    decltype(high) b;

You don't need decltype here. The type is IntT.

    X():a(decltype(a)()),b(decltype(b)())//WHY THIS DOES NOT COMPILE?

Because VS2010 is broken and quite usually won't allow you to use a decltype expression as if it where a type. A typedef before hand might do better.

Luckily you don't need this since you can just use the default constructor rather than the copy.

int _tmain(int argc, _TCHAR* argv[])
{


    X<char,1,'a'> x;//this according to static_assert shouldn't compile but it does

No. The static_assert checks if the types are the same. They are both char with values 1 and 'a'.

    return 0;
}

What you appear to be attempting is to create a template such that the type of the second and third parameters are based on whatever resolved type of the value you pass into it. This can't be done.

随波逐流 2024-10-25 15:49:03

static_assert 确实可以编译,因为模板参数 low 和 high 的 decltype 是 char。查看您的模板定义和实例化。 IntT <-- char

要默认初始化您的成员,您可以这样写:

X():a(),b()
{

The static_assert does compile because the decltype of the template parameters low and high is char. Look at your template definition and the instantiation. IntT <-- char

To default initialize your members you can write this:

X():a(),b()
{
空心↖ 2024-10-25 15:49:03
X():a(decltype(a)()),b(decltype(b)())//WHY THIS DOES NOT COMPILE?

GCC 编译得很好。请参阅: http://www.ideone.com/DG7rt

看起来是 MSVC++10 编译器错误!

X():a(decltype(a)()),b(decltype(b)())//WHY THIS DOES NOT COMPILE?

GCC compiles it fine. See this : http://www.ideone.com/DG7rt

Looks like it's MSVC++10 compiler bug!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文