C++;是否评估了(假)条件? 我学习C++中的模板,C++的术语集有点限制,所以我不能谷歌这个问题。 BR>我尝试实现一个自定义的代码:DATT代码类型:基于 STD::无序的映射图< /代码>。我的目标是能够以如下方式实例化类dict: dict d;//或 dict d;//或 dict d;//或 dict d;//或 dict d;
下面是我使用的代码:C++;是否评估了(假)条件? 我学习C++中的模板,C++的术语集有点限制,所以我不能谷歌这个问题。 BR>我尝试实现一个自定义的代码:DATT代码类型:基于 STD::无序的映射图< /代码>。我的目标是能够以如下方式实例化类dict: dict d;//或 dict d;//或 dict d;//或 dict d;//或 dict d;,c++,c++11,dictionary,conditional-statements,C++,C++11,Dictionary,Conditional Statements,下面是我使用的代码: utils.h #包括 #包括 #包括 #包括 #包括 #包括 //为了勇敢 使用名称空间std; //检查stl向量 //略加修改的版本:https://stackoverflow.com/a/31105859 名称空间是一个容器{ 模板结构stl_向量:false_类型{}; 模板结构stl_向量:真_类型{}; } 命名空间字符串操作{ //基于多个分隔符拆分的通用函数: //资料来源:https://stackoverflow.com/a/9676623 向量拆分(
utils.h
#包括
#包括
#包括
#包括
#包括
#包括
//为了勇敢
使用名称空间std;
//检查stl向量
//略加修改的版本:https://stackoverflow.com/a/31105859
名称空间是一个容器{
模板结构stl_向量:false_类型{};
模板结构stl_向量:真_类型{};
}
命名空间字符串操作{
//基于多个分隔符拆分的通用函数:
//资料来源:https://stackoverflow.com/a/9676623
向量拆分(常量字符串和str,常量字符串和分隔符=“,”){
向量v;
无符号起始=0;
auto pos=str.find_first_of(分隔符,开始);
while(pos!=字符串::npos){
if(pos!=start)//忽略空令牌
v、 后置(str、start、pos-start);
开始=位置+1;
pos=str.find_first_of(分隔符,开始);
}
if(start
main.cpp
#包括
#包括“utils.h”
int main(){
dict d(“/home/path/to/some/file”);
coutif(false)
并不意味着代码没有被编译;它只是意味着里面的代码没有在运行时执行,但它仍然必须是有效的
C++中有(至少)三种条件结构:
预处理器条件。如果条件不满足,则预处理器不将代码传递给编译器。因此,只要代码由有效的预处理器令牌组成,代码就完全是胡乱的。例如,下面是一个格式良好的C++程序,其定义行为:
#include <iostream>
#include <type_traits>
template <typename T>
void print(T x)
{
if constexpr (std::is_same_v<T, int>) {
std::cout << static_cast<typename T::template stack<T>::overflow>(x);
} else {
std::cout << x;
}
}
int main()
{
print("Hello world!\n");
}
(从C++17开始)if constexpr
。此规则有点复杂。必须在编译时知道条件,并且丢弃false
分支。丢弃的分支仍然需要有效,只是没有实例化。因此,如果将if
更改为if const,则上面的代码仍然有效下面是一个格式良好的C++程序,定义行为:
#include <iostream>
#include <type_traits>
template <typename T>
void print(T x)
{
if constexpr (std::is_same_v<T, int>) {
std::cout << static_cast<typename T::template stack<T>::overflow>(x);
} else {
std::cout << x;
}
}
int main()
{
print("Hello world!\n");
}
然后在if constexpr
中使用它来分派:(不要忘记用您正在检查的容器类型替换容器
)
if constexpr(is_std_vector_v){
//做一些特定于病媒的事情
}否则{
//做其他事情
}
未达到条件。事实上,未“达到”任何内容;执行甚至不会启动,因为代码未编译。程序必须格式良好才能编译-包括在运行时不会执行的部分。如果(false){random garbage},则无法编写
并期望代码能够编译。实现(我认为)您试图实现的目标的一种方法是通过traits类,该类具有统一的接口,并为您想要支持的每个容器专门化。“不可访问”是运行时的事情。在代码能够运行之前,必须对其进行编译。为了进行编译,它必须(除其他外)语法上有效并通过类型检查。if(false){int x=“apples”*1.5;}
是一个类型错误,因此它不会通过编译。if(false){fjakslfkdjfaklsdfjkalskdjf();}
使用未声明的标识符,因此无法通过编译。我不理解该注释。错误的代码不会因为您移动它而突然编译。无论您是否提及它,这就是if(false){}
的意思。它意味着“在程序运行时不执行此块”。它并不意味着“甚至不要查看此块中的代码”。@7kemZmani在您的
#include <iostream>
#include <climits>
int main()
{
if (false) {
volatile int x = 42/0;
x = *static_cast<int*>(nullptr);
const int y = INT_MAX + 1;
} else {
std::cout << "Hello world!\n";
}
}
#include <iostream>
#include <type_traits>
template <typename T>
void print(T x)
{
if constexpr (std::is_same_v<T, int>) {
std::cout << static_cast<typename T::template stack<T>::overflow>(x);
} else {
std::cout << x;
}
}
int main()
{
print("Hello world!\n");
}
template <typename C>
struct is_std_vector :std::false_type {};
template <typename T, typename A>
struct is_std_vector<std::vector<T, A>> :std::true_type {};
template <typename C>
inline constexpr bool is_std_vector_v = is_std_vector<C>::value;
if constexpr (is_std_vector_v<Container>) {
// do std::vector specific things
} else {
// do other things
}