ustone 发表于 2024-10-28 13:39:26

C++20标准对C++14的兼容性如何? C++14项目升级C++20需要修改代码吗?

C++20标准对C++14的兼容性如何?
C++14项目升级C++20需要修改代码吗?

JuncoJet 发表于 2024-10-28 13:59:55

应该没问题,编译下试试
就算有问题,也能通过写成动态库,导出接口

scoopydoo 发表于 2024-10-28 14:50:12

C++ 太过复杂了,没人敢说精通 C++ 的所有方面,估计在本坛敢说自己熟练掌握 C++ 的都没有。

不过你担心的兼容性问题,俺觉得不会有什么大麻烦,毕竟保持向下兼容性是任何语言版本升级首先要考虑的事情 (Python 除外 ;P )。

chipset009 发表于 2024-10-28 16:37:55

不同的编译器不一样。例如gcc(g++)编译的时候,后面方框里打个勾,或者命令行指定-std=c++11。
其实根本不用关注那么多,大多数情况C++11就行,2024年了,大部分编译器对C++17支持的都残缺不全。

ustone 发表于 2024-10-28 16:47:30

JuncoJet 发表于 2024-10-28 13:59
应该没问题,编译下试试
就算有问题,也能通过写成动态库,导出接口

现在没有代码,chipset009那本书是C++14的

ustone 发表于 2024-10-28 16:53:18

scoopydoo 发表于 2024-10-28 14:50
C++ 太过复杂了,没人敢说精通 C++ 的所有方面,估计在本坛敢说自己熟练掌握 C++ 的都没有。

不过你担心 ...

这些特性基本都用不到,C++14有的那些特性也都被后续标准继续支持
我比较担心的是STL标准模板库的用法C++20对C++14是不是都兼容

ustone 发表于 2024-10-28 16:56:50

chipset009 发表于 2024-10-28 16:37
不同的编译器不一样。例如gcc(g++)编译的时候,后面方框里打个勾,或者命令行指定-std=c++11。
其实根本不 ...

常用的特性没有变化就可以了,C++11好像有个别特性被后来的标准废弃或者修改

chipset009 发表于 2024-10-28 17:05:11

ustone 发表于 2024-10-28 16:56
常用的特性没有变化就可以了,C++11好像有个别特性被后来的标准废弃或者修改

Unicode,后来去掉了,最蛋疼的就这玩意了,再后来标准貌似没有加上。
STL不用担心,C++23一路能兼容到C++98,只是越后来写法越简单而已。

C++里复杂的地方有几处,重载协议,模板,并发控制(原子操作,mutex,条件变量...)。

ustone 发表于 2024-10-28 17:12:58

chipset009 发表于 2024-10-28 17:05
Unicode,后来去掉了,最蛋疼的就这玩意了,再后来标准貌似没有加上。
STL不用担心,C++23一路能兼容到C ...

我写的小程序代码基本还是按C++98标准,不过要用一些STL库,曾经发现random_shuffle函数用法C++11跟C++98好像不太一样

chipset009 发表于 2024-10-28 17:14:52

C++是个讲人权的地方,你想怎么样都行,但后果自负。所以我觉得,能用C则用C,能用JAVA或则用JAVA或者C#,能用脚本则用脚本,除非迫不得已,否则还是远离C++比较好。

举个例子吧,调用一个函数,这玩意简单的不能再简单了。在C++里看上去也有点复杂,你可以不那么写,但你约束不了别人怎么写。
int foo(int n)
{
int s = 1;
for(; 0 < n; --n)
    s *= n;
return s;
}

class C
{
public:
C() = default;
explicit C(int a) : y(a) {}
~C() = default;
int foo(int x) const { return x + y; }

private:
int y;
};

#include <functional>
#include <string>
#include <iostream>

int main()
{
typedef int (*f_int_t1) (int); // the old style
f_int_t1 foo_p1 = &foo;      // & is redundant and optional
using f_int_t2 = int (*)(int); // easy to understand
f_int_t2 foo_p2 = foo;
std::function<int(int)> stdf_foo = &foo; // modern

const std::string Sep(" ");
constexpr int N = 5;

std::cout << "global function:" << foo(N) << Sep << foo_p1(N) << Sep
            << foo_p2(N) << Sep << stdf_foo(N) << "\n";

typedef int (C::* f_C_int_t1) (int) const;
f_C_int_t1 C_foo_p1 = &C::foo;// the old style
using f_C_int_t2 = int (C::*) (int) const;
f_C_int_t2 C_foo_p2 = &C::foo;

const C c(10);
constexpr int X = 20;

std::cout << "old member call: " << (c.*C_foo_p1)(X) << Sep << (c.*C_foo_p2)(X) << "\n";

std::function<int(const C&, int)> stdf_C_foo = &C::foo;
auto greet = std::mem_fn(&C::foo);
auto callback = std::bind(&C::foo, &c, std::placeholders::_1);
std::cout << "new member call: " << stdf_C_foo(c, X) << Sep << greet(c, X) << Sep
            << callback(X) << "\n";

return 0;
}

scoopydoo 发表于 2024-10-28 17:17:22

ustone 发表于 2024-10-28 16:47
现在没有代码,chipset009那本书是C++14的

若你只是拿来学习或者做些小的实用工具,完全不必担心这些东西 ......

scoopydoo 发表于 2024-10-28 17:19:40

chipset009 发表于 2024-10-28 17:14
C++是个讲人权的地方,你想怎么样都行,但后果自负。所以我觉得,能用C则用C,能用JAVA或则用JAVA或者C#, ...

俺完全同意你的观点,但是不赞同你贴代码的方式! :lol

int foo(int n)
{
int s = 1;
for(; 0 < n; --n)
    s *= n;
return s;
}

class C
{
public:
C() = default;
explicit C(int a) : y(a) {}
~C() = default;
int foo(int x) const { return x + y; }

private:
int y;
};

#include <functional>
#include <string>
#include <iostream>

int main()
{
typedef int (*f_int_t1) (int); // the old style
f_int_t1 foo_p1 = &foo;      // & is redundant and optional
using f_int_t2 = int (*)(int); // easy to understand
f_int_t2 foo_p2 = foo;
std::function<int(int)> stdf_foo = &foo; // modern

const std::string Sep(" ");
constexpr int N = 5;

std::cout << "global function:" << foo(N) << Sep << foo_p1(N) << Sep
            << foo_p2(N) << Sep << stdf_foo(N) << "\n";

typedef int (C::* f_C_int_t1) (int) const;
f_C_int_t1 C_foo_p1 = &C::foo;// the old style
using f_C_int_t2 = int (C::*) (int) const;
f_C_int_t2 C_foo_p2 = &C::foo;

const C c(10);
constexpr int X = 20;

std::cout << "old member call: " << (c.*C_foo_p1)(X) << Sep << (c.*C_foo_p2)(X) << "\n";

std::function<int(const C&, int)> stdf_C_foo = &C::foo;
auto greet = std::mem_fn(&C::foo);
auto callback = std::bind(&C::foo, &c, std::placeholders::_1);
std::cout << "new member call: " << stdf_C_foo(c, X) << Sep << greet(c, X) << Sep
            << callback(X) << "\n";

return 0;
}

scu319hy 发表于 2024-10-28 19:21:40

scoopydoo 发表于 2024-10-28 14:50
C++ 太过复杂了,没人敢说精通 C++ 的所有方面,估计在本坛敢说自己熟练掌握 C++ 的都没有。

不过你担心 ...

"精通"这个词太不精确了。又不能当饭吃,要那么"精通"干什么:lol

在C++11之前,我有事没事就翻iso14882。背下来不敢说,但哪个特性在哪一章,有哪些相关条款,我都能记个大概。这算"精通"嘛?;P
这个不算的话,我还手撸过C++前端,C++98的语法基本上支持得七七八八了。GCC和Clang咱也改过些许。
实在不行还可以算上我自己写过汇编器,链接器和调试器。再不行算上我自己写的CRT和部分STL呢?

但上面那一堆东西都没给我换来一顿饭吃。最近感觉很多基本语法都已经开始遗忘了。
有生之年我估计能做到手中没有C++,心中也没有C++的至高境界了:lol

scoopydoo 发表于 2024-10-28 20:24:35

scu319hy 发表于 2024-10-28 19:21
"精通"这个词太不精确了。又不能当饭吃,要那么"精通"干什么

在C++11之前,我有事没事就翻iso148 ...

你已经很厉害了,但是,你甚至都没达到熟练掌握 C++ 的程度。 ;P

举个栗子吧,作为中国人,母语是汉语,大部分成年人算是熟练掌握汉语,极少数语言学专家算是精通汉语。

量子隧道 发表于 2024-10-28 20:39:38

吾生也有涯,而知也无涯。以有涯随无涯,殆已!还在追求C++的人,殆而已矣!坚决拒绝死嚼C加加。编程语言这些玩意儿对我来说就是让芯片转起来,让仿真跑起来的工具。
页: [1] 2
查看完整版本: C++20标准对C++14的兼容性如何? C++14项目升级C++20需要修改代码吗?