是否可以在C ++中在switch的范围外声明变量? [英] Is it possible to declare variable outside of switch scope in C++?

查看:300
本文介绍了是否可以在C ++中在switch的范围外声明变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总之,请考虑下面的(伪)代码:

In short, think about the (pseudo-)code below:

switch (n) {
    case 15:
        (keyword) customtemplate<15> t_var; /* I want it to be outside of switch */
        break;
    case 255:
        (keyword) customtemplate<255> t_var; /* I want it to be outside of switch */
        break;
    default:
        break;
}

t_var.do_something();

我想知道是否有一些(关键字)使下面的变量是全局的,或者在switch-case范围之外。

I wonder if there is some (keyword) that makes a following variable be global, or outside of switch-case scope.

我想要这么奇怪的代码的原因是,我不能声明模板变量任意数目的n,即:

The reason that I want such a weird code is, I can't declare template variable with arbitrary number of n, i.e:

int n = 15; // or int n = 255;
custometemplate<n> t_var; /* I can't do this */
t_var.do_something;

本文中提到的 customtemplate RS ezpwd-reed -solomon 。我想声明 RS <15,2> ,<$ c之一 RS $ c> RS <15,4> , RS <15,7> RS <15,11> / code>, RS <64,32> RS <160,128> 。 >

The customtemplate mentioned in this post is acutally RS<n,k> in ezpwd-reed-solomon. I want to declare RS<n, k> which is one of RS<15,2>, RS<15, 4>, RS<15, 7>, RS<15, 11>, RS<64, 32>, RS<160, 128>.

推荐答案

简单的回答是NO!

模板实例只是一个不同的类型。

In your case a different template instance is simply a different type.

如果你可以做你想要的东西,问题出现在这个任何类型的实例。

If you can do something like you want, the question occurs what to do with this instance of any type. How would you later use this variable?

记住:创建模板实例是在编译期间完成的!

Remember: Creating a template instance is done during compile time! Running through a switch case is done in runtime.

您的代码生成的问题与

switch (foo)
{
    case 1:
       int bar;
       break;

    case 2:
       float bar;
       break;
 }

 ??? and now how the compiler should handle two different types ???

再次:模板实例是不同的类型!

Again: Template instances are different types!

正如别人在这里提到的,你可以使用一个变体类型,它只包含给定类型列表的一个实例。它还包含一个类型实际存储在此变体实例中的信息。

As others already mention here, you can use a variant type which contains exactly one instance of a list of given types. And it also contains a information which type is actually stored in this variant instance.

但我更喜欢使用可用的实现,而不是写我自己的。为此。 Boost变体 http://www.boost.org/doc/libs /1_58_0/doc/html/variant.html

But I prefer using a available implementation instead of writing my own. For this see e.g. Boost Variant http://www.boost.org/doc/libs/1_58_0/doc/html/variant.html

但是作为我的经验的简历:如果你觉得你需要一个变体类型,你已经强烈证明您的设计。作为提示:如果你需要一个变体类型,你以后会看到,你每次处理这个变体,你必须检查实际内容。我相信这通常是违反了良好的OOP设计。它可以帮助,但我认为应该避免!

But as a resume from my experience: If you feel that you need a variant type you have strongly prove your design. As a hint: If you need a variant type you later will see that you every time you handle this variant you have to check for the actual content. I believe that this is often a violation of a good OOP design. It can help but for my opinion it should be avoided!

这篇关于是否可以在C ++中在switch的范围外声明变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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