August 2022 Archives

对于特定的函数或者变量属性检测一个特定的参数类:

#include <iostream>
#include <boost/smart_ptr.hpp>

#include <type_traits>
using namespace boost;
using namespace std;

#define _CAT(A, B) A##B
#define STR(s) #s
#define CAT(A,B) _CAT(A,B)
#define CHECKER_PREFIX __has_member_function_
#define HAS_MEMBER_FUNC_CHECKER_NAME(FUNC) CAT(CHECKER_PREFIX, FUNC)

#define HAS_MEMBER_FUNC_CHECKER(FUNC) template <typename T> \
struct HAS_MEMBER_FUNC_CHECKER_NAME(FUNC){ \
template <typename C> static std::true_type test(decltype(&C::FUNC)); \
template <typename C> static std::false_type test(...); \
static constexpr bool value = std::is_same<decltype(test<T>(nullptr)),std::true_type>::value; \
};

#define HAS_MEMBER_FUNC(TYPE, FUNC) (HAS_MEMBER_FUNC_CHECKER_NAME(FUNC)<TYPE>::value)


class TestA{
public:
   int* foo(){

       int* a =new int(1);
       return a;

  }

   static int foo2(){
       return  1;
  }

   static const int foo3;
};

HAS_MEMBER_FUNC_CHECKER(foo);
HAS_MEMBER_FUNC_CHECKER(foo2);
HAS_MEMBER_FUNC_CHECKER(foo3);
int main() {

   static_assert(HAS_MEMBER_FUNC(TestA, foo));
   std::cout<<HAS_MEMBER_FUNC(TestA, foo) <<std::endl;

   return 0;
}

对于确定参数类型的泛型成员函数:

#include <iostream>
#include <boost/smart_ptr.hpp>

#include <type_traits>
using namespace boost;
using namespace std;

#define _CAT(A, B) A##B
#define STR(s) #s
#define CAT(A,B) _CAT(A,B)
// 对于固定个数的模版类,检测某一个属性或者函数是否存在
#define CHECKER_PREFIX2 _has_member_fcuntoin2_
#define HAS_MEMBER_FUNC_CHECK_NAME2_(FUNC) CAT(CHECKER_PREFIX2,FUNC)

#define HAS_MEMBER_FUNC_CEHCKER2(FUNC) template<typename T, typename R> \
   struct HAS_MEMBER_FUNC_CHECK_NAME2_(FUNC){ \
   template<typename C> static std::true_type test(decltype(&C::template FUNC<R>)); \
   template<typename C> static std::false_type test(...); \
   static constexpr bool value = std::is_same<decltype(test<T>(nullptr)),std::true_type>::value; \
}

// #define IF_HAS_MEMBER_FUNC2(FUNC,CLASS_,CLASS_F_T) (HAS_MEMBER_FUNC_CEHCK_NAME2_(FUNC)<CLASS_ ,CLASS_F_T>::value)


class TestA{
public:
   int* foo(){

       int* a =new int(1);
       return a;

  }

   static int foo2(){
       return  1;
  }

   template <typename T>
   static  int foo4(){
       return 1;
  }

   static const int foo3;
};


//含有固定类型的模版类
HAS_MEMBER_FUNC_CEHCKER2(foo4);

int main() {
   // 输出 1
   std::cout<< HAS_MEMBER_FUNC_CHECK_NAME2_(foo4)<TestA,char>::value << std::endl;
   // 输出 1
   std::cout<< HAS_MEMBER_FUNC_CHECK_NAME2_(foo4)<TestA,int>::value << std::endl;

   return 0;
}

See also:

https://blog.csdn.net/TH_NUM/article/details/90968219

https://harrychen.xyz/2019/06/04/cpp-17-mock-concept/

Linux磁盘分区问题

当前fdisk查看的磁盘分区比实际df出来的磁盘分区大,实际磁盘分区不可用。

[SME@search-10 ~]$ df -Th
Filesystem     Type     Size Used Avail Use% Mounted on
devtmpfs       devtmpfs   94G     0   94G   0% /dev
tmpfs         tmpfs     94G     0   94G   0% /dev/shm
tmpfs         tmpfs     94G   18M   94G   1% /run
tmpfs         tmpfs     94G     0   94G   0% /sys/fs/cgroup
/dev/sda3     ext4      7.6G  2.6G  4.6G  36% /
/dev/sda2     ext4      2.0G 389M  1.5G  22% /boot
/dev/sda4     ext4      1.7T   60K  1.6T   1% /home/search
tmpfs         tmpfs     19G     0   19G   0% /run/user/0
tmpfs         tmpfs     19G     0   19G   0% /run/user/1006

[SME@search-10 ~]$ sudo fdisk -l
Disk /dev/sda: 2.6 TiB, 2876997894144 bytes, 5619136512 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 262144 bytes / 786432 bytes
Disklabel type: gpt
Disk identifier: 40992439-8FB1-4EF3-AF5D-0B180C07EA05

Device         Start       End   Sectors Size Type
/dev/sda1        2048       4095       2048   1M BIOS boot
/dev/sda2        4096    4198399    4194304   2G Linux filesystem
/dev/sda3     4198400 2101350399 2097152000 1000G Linux filesystem
/dev/sda4  2101350400 5619003391 3517652992  1.7T Microsoft basic data
/dev/sda5  5619003392 5619136478     133087   65M Linux filesystem

由于是根分区,无法重新格式化,且重新通过分配之后,重启也无法获得扩展,如下所示:

[SME@search-10 ~]$ sudo fdisk /dev/sda

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/sda: 2.6 TiB, 2876997894144 bytes, 5619136512 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 262144 bytes / 786432 bytes
Disklabel type: gpt
Disk identifier: 40992439-8FB1-4EF3-AF5D-0B180C07EA05

Device         Start       End   Sectors Size Type
/dev/sda1        2048       4095       2048   1M BIOS boot
/dev/sda2        4096    4198399    4194304   2G Linux filesystem
/dev/sda3     4198400 2101350399 2097152000 1000G Linux filesystem
/dev/sda4  2101350400 5619003391 3517652992  1.7T Microsoft basic data
/dev/sda5  5619003392 5619136478     133087   65M Linux filesystem

Command (m for help): d
Partition number (1-5, default 5): 3

Partition 3 has been deleted.

Command (m for help): p
Disk /dev/sda: 2.6 TiB, 2876997894144 bytes, 5619136512 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 262144 bytes / 786432 bytes
Disklabel type: gpt
Disk identifier: 40992439-8FB1-4EF3-AF5D-0B180C07EA05

Device         Start       End   Sectors Size Type
/dev/sda1        2048       4095       2048   1M BIOS boot
/dev/sda2        4096    4198399    4194304   2G Linux filesystem
/dev/sda4  2101350400 5619003391 3517652992  1.7T Microsoft basic data
/dev/sda5  5619003392 5619136478     133087   65M Linux filesystem

Command (m for help): n
Partition number (3,6-128, default 3):
First sector (4198400-2101350399, default 4198400):
Last sector, +sectors or +size{K,M,G,T,P} (4198400-2101350399, default 2101350399):

Created a new partition 3 of type 'Linux filesystem' and of size 1000 GiB.
Partition #3 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: Y

The signature will be removed by a write command.

Command (m for help): p
Disk /dev/sda: 2.6 TiB, 2876997894144 bytes, 5619136512 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 262144 bytes / 786432 bytes
Disklabel type: gpt
Disk identifier: 40992439-8FB1-4EF3-AF5D-0B180C07EA05

Device         Start       End   Sectors Size Type
/dev/sda1        2048       4095       2048   1M BIOS boot
/dev/sda2        4096    4198399    4194304   2G Linux filesystem
/dev/sda3     4198400 2101350399 2097152000 1000G Linux filesystem
/dev/sda4  2101350400 5619003391 3517652992  1.7T Microsoft basic data
/dev/sda5  5619003392 5619136478     133087   65M Linux filesystem

Filesystem/RAID signature on partition 3 will be wiped.

Command (m for help): w
The partition table has been altered.
Syncing disks.

[SME@search-10 ~]$ sudo partprobe /dev/sda
Warning: The disk CHS geometry (11017914,255,2) reported by the operating system does not match the geometry stored on the disk label (22095,255,63).
Error: Can't have overlapping partitions.

于是,尝试使用resize2fs,如下所示:

[SME@search-10 ~]$ sudo resize2fs /dev/sda3
resize2fs 1.45.6 (20-Mar-2020)
Filesystem at /dev/sda3 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 125
The filesystem on /dev/sda3 is now 262144000 (4k) blocks long.

[SME@search-10 ~]$ df -Th
Filesystem     Type     Size Used Avail Use% Mounted on
devtmpfs       devtmpfs   94G     0   94G   0% /dev
tmpfs         tmpfs     94G     0   94G   0% /dev/shm
tmpfs         tmpfs     94G   18M   94G   1% /run
tmpfs         tmpfs     94G     0   94G   0% /sys/fs/cgroup
/dev/sda3     ext4     985G  2.6G 942G   1% /
/dev/sda2     ext4      2.0G 389M  1.5G  22% /boot
/dev/sda4     ext4      1.7T   60K  1.6T   1% /home/search
tmpfs         tmpfs     19G     0   19G   0% /run/user/0

结果成功扩展了分区。

Monthly Archives

Pages

Powered by Movable Type 7.9.4

About this Archive

This page is an archive of entries from August 2022 listed from newest to oldest.

May 2022 is the previous archive.

November 2022 is the next archive.

Find recent content on the main index or look in the archives to find all content.