受这个问题的启发,我想知道是否有一些编译时检查可以引入检测两个给定的模板实例:
template <typename T>
class Templ...
typedef Templ<std::string> stringInstance;
typedef Templ<double> doubleInstance;
Run Code Online (Sandbox Code Playgroud)
是从相同的定义构造的,或者如果它们是从Templ模板的不同特化构建的
所以基本上假设的模板函数将表现如下:
template <typename T>
class Templ
{}
template <>
class Templ<std::string>
{}
template <>
class Templ<double>
{}
template <typename T1,typename T2>
class Belong_To_Same_Templ_Definition
{}
//tests
typedef Templ<std::string> stringInstance;
typedef Templ<double> doubleInstance;
typedef Templ<int> intInstance;
typedef Templ<char> charInstance;
assert( Belong_To_Same_Templ_Definition< intInstance , charInstance >::value == true);
assert( Belong_To_Same_Templ_Definition< intInstance , doubleInstance >::value == false);
assert( Belong_To_Same_Templ_Definition< stringInstance , doubleInstance >::value == false);
Run Code Online (Sandbox Code Playgroud)
有可能创造这种元功能吗?
c++ static-assert compile-time template-specialization template-meta-programming
如题.
当使用该std::get<T>(pair)对的第一个成员是const时,会发生此编译错误,该函数来自std::map或的迭代器std::unordered_map.
要测试编译错误,请注释掉"notstd"重载get.
我已经在Stack Overflow上研究了这个问题,下面列出了三个最相关的问题.
现有的答案让我相信它应该是一个缺陷报告,应该将相应的std::get重载添加到标准库中,并且应该扩展应用于临时常量引用的自动生命周期扩展以涵盖这种情况.
我还研究了它是否与布局的专业化有关(问题14272141,下面链接).但是,我的代码片段仅要求对两个成员之一的const引用; 即使布局专门化,对任一成员的const引用仍应存在.
根据现有的答案,我理解在两者之间铸造const std::pair<T, U>&并且const std::pair<const T, U>&不安全.
namespace notstd
{
template <class T, class U>
const T& get(const std::pair<const T, U>& tu)
{
return tu.first;
}
}
int test(int value)
{
using namespace notstd;
using namespace std;
const std::pair<const int, bool> one(value, false);
const auto two = get<int>(one);
const auto three = get<const int>(one);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
相关性高的问题: