我想知道为什么在编译时初始化整数protected void Page_Load(object sender, EventArgs e)
{
if(!String.IsNullOrEmpty(Request.QueryString["srch"]))
{
String srch = Request.QueryString["srch"];
if (!IsPostBack)
{
var myControl = (TextBox)Master.FindControl("search");
myControl.Text = srch;
}
//perform search and display results
String connString = System.Configuration.ConfigurationManager.ConnectionStrings["GroupsConnString"].ToString();
conn = new MySql.Data.MySqlClient.MySqlConnection(connString);
conn.Open();
queryStr = "";
queryStr = "SELECT g.*,CONCAT(firstname,' ',lastname) as name FROM app_groups.groups g LEFT JOIN users u ON u.id = g.id_user WHERE group_name LIKE '%" + srch + "%' OR group_type LIKE '%" + srch + "%'";
cmd = new MySql.Data.MySqlClient.MySqlCommand(queryStr, conn);
reader = cmd.ExecuteReader();
gname = "";//group name
gtype = "";//group type
uname = "";//user name
id = "";
warning = "";
id_owner = "";
if (reader.HasRows)
{
while (reader.HasRows && reader.Read())
{
uname = reader.GetString(reader.GetOrdinal("name"));
gname = reader.GetString(reader.GetOrdinal("group_name"));
gtype = reader.GetString(reader.GetOrdinal("group_type"));
id = reader.GetString(reader.GetOrdinal("id"));
id_owner = reader.GetString(reader.GetOrdinal("id_user"));
warning = reader.GetString(reader.GetOrdinal("warning"));
Panel pan = new Panel();
pan.CssClass = "col-sm-4";
Panel subpan = new Panel();
subpan.CssClass = "group";
if (Session["uid"] != null)
{
int sess_id = Convert.ToInt32(Session["uid"]);
if ((Session["uid"] != null && Session["uadmin"].Equals("1")) || sess_id.Equals(Int32.Parse(id_owner)))
{
adminCtrls = new Panel();
adminCtrls.CssClass = "adminCtrls";
btn = new Button();//delete group button for admins and owners only
btn.Click += new EventHandler(deleteGroup);
btn.ID = "sterge";
btn.CssClass = "btn btn-danger btn-xs";
btn.Text = "Sterge grup";
btn.CommandArgument = id;
//btn.Attributes.Add("style", "float:right;");
adminCtrls.Controls.Add(btn);
if (Session["uadmin"].Equals("1"))
{
if (!warning.Equals("1"))
{
btn = new Button();//warn owner button for admins only
btn.Click += new EventHandler(warnOwner);
btn.ID = "warn";
btn.CssClass = "btn btn-warning btn-xs";
btn.Text = "Avertizeaza owner grup";
btn.CommandArgument = id;
adminCtrls.Controls.Add(btn);
}
else
{
btn = new Button();//warn owner button for admins only
btn.ID = "warn";
btn.Enabled = false;
btn.CssClass = "btn btn-warning btn-xs";
btn.Text = "A fost avertizat";
adminCtrls.Controls.Add(btn);
}
}
}
else
{
adminCtrls = null;
}
if (adminCtrls != null)
{
subpan.Controls.Add(adminCtrls);
}
}
subpan.Controls.Add(new LiteralControl("<span class='title text-center'>" + gname + "</span><br/>"));
subpan.Controls.Add(new LiteralControl("<span>Owner: <span class='owner'>" + uname + "</span></span><br/>"));
subpan.Controls.Add(new LiteralControl("<span>Categorie: <span class='type'>" + gtype + "</span></span><br/>"));
pan.Controls.Add(subpan);
groupsShow.Controls.Add(pan);
}
}
reader.Close();
conn.Close();
}
}
,而不是在这里浮点ii
:
ff
当我尝试编译时会发生这种情况:
int main() {
const int i = 1;
constexpr int ii = i;
const float f = 1.0;
constexpr float ff = f;
}
答案 0 :(得分:15)
具有常量初始值设定项的整数类型的常量变量是整型常量表达式(事实上隐含式constexpr
;请参阅ISO C ++中的expr.const)。 float
不是整数类型,不符合{em>常量表达式的要求而不使用constexpr
。 (类似的情况是int
可以的原因,但float
不能是模板参数。)
答案 1 :(得分:10)
在C ++中,常量整数的处理方式与其他常量类型不同。如果使用编译时常量表达式初始化它们,则可以在编译时表达式中使用它们。这样做是为了使数组大小可以是const int
而不是#define
d(就像你在C中被强制一样):
(假设没有VLA扩展名)
const int s = 10;
int a[s]; // OK in C++