Ste*_*mer 7 c++ constexpr c++11
我有以下示例代码,它使用字符串文字作为模板参数,以便基类模板可以访问该字符串.
代码编译,但我得到一个我不完全理解的警告:
警告:'ns :: bar :: type'有一个基数'ns :: base <((const char*)(&ns :: bar :: name))>',其类型使用匿名命名空间[默认启用]
下面的工作示例代码:
// "test.h"
#pragma once
namespace ns
{
template <char const* str>
struct base
{
const char *name() const { return str; }
};
namespace bar
{
static constexpr char name[] = "bar";
struct type : base<name> {}; // <-- this line here
}
}
// main.cpp
#include <iostream>
#include "test.h"
int main()
{
ns::bar::type f;
std::cout << f.name() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:
(注意这是用gcc 4.7.2)
问题在于static constexpr char name[] = "bar";具有内部联系的事实.
在标头中定义的具有内部链接的任何类型在包含标头的每个文件中都是不同的类型.
这很少是意图 - 因此警告.
在源文件中完成此操作时没有警告的原因是因为该类型不能被多个文件引用 - 因此它始终是一种类型.
| 归档时间: |
|
| 查看次数: |
2619 次 |
| 最近记录: |