RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-20575

OlegUP's questions

Martin Hope
OlegUP
Asked: 2022-08-29 15:06:59 +0000 UTC

C++ Typelist 和可变参数模板绑定元函数。编译时出错

  • 0

我Typelist用于分组类型。为此,我实现了一个元函数tlist_find_if,它接受一个谓词Pred和一个类型列表作为输入,如果条件匹配,则返回列表中包含的类型。

// template <typename... Ts>
struct tlist
{
   using type = tlist;
   static constexpr size_t size() noexcept { return sizeof...(Ts); }
};
    
template <template<typename T> typename Pred, typename TList>
struct tlist_find_if_impl;

template <template<typename T> typename Pred>
struct tlist_find_if_impl<Pred, tlist<>> { using result = null_t; };

template <template<typename T> typename Pred, typename H, typename... Ts>
struct tlist_find_if_impl<Pred, tlist<H, Ts...>>
{   
    using result = typename std::conditional<Pred<H>::value, H, typename tlist_find_if_impl<Pred, tlist<Ts...> >::result>::type;
};

// Wrapper
template <template <typename T> typename Pred, typename TList>
struct tlist_find_if;

template <template <typename T> typename Pred, typename... Ts>
struct tlist_find_if<Pred, tlist<Ts...>>
{   
    using result = typename tlist_find_if_impl<Pred, tlist<Ts...> >::result;
};

我还实现了一个元函数bind来组合谓词:

template <template <typename...> typename F, typename T>
struct bind
{
    template <typename... Ts> 
    using type = F<T, Ts...>;
};

但是在使用它时,编译器要么接受所有内容,要么给出错误。

没有错误的示例:

int main()
{
    // ...
    using type = tlist_find_if<bind<std::is_same, a1_t>::type, x_group >::result; // , x_group>;
    static_assert(std::is_same_v<a1_t, type>);
}

与特定类型一起使用时没有错误。但是在模板元函数中使用时,编译器会抛出错误。

    template <typename Group>
    struct type_in_group_pred
    {            
        static constexpr bool value = std::is_same<Group, typename tlist_find_if<bind<std::is_same, T>::type, Group>::result>::value; // Ошибка!
    };
    using group = typename tlist_find_if<type_in_group_pred, GroupList>::result;

错误文字:

main2.cc:107:117: error: type/value mismatch at argument 1 in template parameter list for ‘template<template<class T> class Pred, class TList> struct tlist_find_if’
  107 |         static constexpr bool value = std::is_same<Group, typename tlist_find_if<bind<type_in_group, T>::type, Group>::result>::value;
      |                                                                                                                     ^
main2.cc:107:116: error: type/value mismatch at argument 1 in template parameter list for ‘template<template<class T> class Pred, class TList> struct tlist_find_if’
  107 |         static constexpr bool value = std::is_same<Group, typename tlist_find_if<bind<std::is_same, T>::type, Group>::result>::value;
      |                                                                                                                    ^
main2.cc:107:116: note:   expected a class template, got ‘bind<std::is_same, T>::type’

也就是说,由于某种原因,编译器将 bind 的结果读取为具体类型,而不是模板。但为什么?在没有错误的示例中,一切正常。

供复制的样本测试程序的全文

#include <type_traits>
#include <stdio.h>:

struct a1_t { int x; };
struct a2_t { int x; };
struct b1_t { int y; };
struct b2_t { int y; };
struct c1_t { int z; };
struct c2_t { int z; };
struct null_t {};


template <typename... Ts>
struct tlist
{
   using type = tlist;
   static constexpr size_t size() noexcept { return sizeof...(Ts); }
};

template <template<typename T> typename Pred, typename TList>
struct tlist_find_if_impl; // { using result = null_t; };

template <template<typename T> typename Pred>
struct tlist_find_if_impl<Pred, tlist<>> { using result = null_t; };

template <template<typename T> typename Pred, typename H, typename... Ts>
struct tlist_find_if_impl<Pred, tlist<H, Ts...>>
{
    using result = typename std::conditional<Pred<H>::value, H, typename tlist_find_if_impl<Pred, tlist<Ts...> >::result>::type;
};

// Wrapper
template <template <typename T> typename Pred, typename TList>
struct tlist_find_if;

template <template <typename T> typename Pred, typename... Ts>
struct tlist_find_if<Pred, tlist<Ts...>>
{
    using result = typename tlist_find_if_impl<Pred, tlist<Ts...> >::result;
};

template <typename T, typename U>
struct null_t_wrap
{
    using type = typename std::conditional<std::is_same_v<null_t, T>, U, T>::type;
};

template <template <typename...> typename F, typename T>
struct bind
{
    template <typename... Ts>
    using type = F<T, Ts...>;
};

using null_group = tlist<>;
using x_group = tlist<a1_t, a2_t>;
using y_group = tlist<b1_t, b2_t>;
using group_tlist = tlist<x_group, y_group>;

template <typename T>
struct null_setter { static void set(T& t, int x) { (void)t; (void)x; printf("null setter x = %d\n", x); } };

template <typename T>
struct x_setter { static void set(T& t, int x) { t.x = x; printf("set x = %d\n", x); } };

template <typename T>
struct y_setter { static void set(T& t, int y) { t.y = y; printf("set y = %d\n", y); } };

template <typename T, typename Group>
struct group_2_setter { using setter = null_setter<T>;  };
template <typename T>
struct group_2_setter<T, x_group> { using setter = x_setter<T>; };
template <typename T>
struct group_2_setter<T, y_group> { using setter = y_setter<T>; };

template <typename T, typename Group>
struct type_2_group 
{ 
    template <typename H>
    struct type_is_same_pred
    {
        static constexpr bool value = std::is_same<T, H>::value;
    };
    // using group = typename null_t_wrap<typename tlist_find_if<type_is_same_pred, Group>::result, null_group>::type;                       
    using group = typename tlist_find_if<type_is_same_pred, Group>::result;                       
};

template <typename T, typename Group>
struct type_in_group
{
    template <typename U>
    struct is_t
    {
        static constexpr bool value = std::is_same_v<T, U>; 
    };
    static constexpr bool value = std::is_same<T, typename tlist_find_if<is_t, Group>::result>::value;
};

template <typename T, typename GroupList>
struct group_list_2_group 
{ 
    template <typename Group>
    struct type_in_group_pred
    {
        // static constexpr bool value = std::is_same<Group, typename type_2_group<T, Group>::group >::value;
        
        static constexpr bool value = std::is_same<Group, typename tlist_find_if<bind<std::is_same, T>::type, Group>::result>::value;
    };
   // using group = typename null_t_wrap<typename tlist_find_if<type_in_group_pred, GroupList>::result, null_group>::type;
//    using group = typename tlist_find_if<type_in_group_pred, GroupList>::result;
    using group = typename tlist_find_if<type_in_group_pred, GroupList>::result;
};

template <typename T, typename GroupList>
struct type_2_setter 
{
    using setter = typename group_2_setter<T, typename group_list_2_group<T, GroupList>::group>::setter; 
};

int main()
{
    a1_t a1; 
    a2_t a2;
    b1_t b1;
    b2_t b2;
    c1_t c1;
    c2_t c2;

    using is_same_as_a1_t = bind<std::is_same, a1_t>::type<a1_t>;
    static_assert(is_same_as_a1_t::value);
//    using a1_t_setter = type_2_setter<a1_t, group_tlist>::setter;
//    a1_t_setter::set(a1, 1);
//  using group = type_2_group<a1_t, x_group>;
//  printf("%s\n", typeid(group).name());
//  static_assert(std::is_same_v<group, null_t>);
    
//    using a2_t_setter = type_2_setter<a2_t, group_tlist>::setter;
//    a2_t_setter::set(a2, 2);
        
//    using b1_t_setter = type_2_setter<b1_t, group_tlist>::setter;
//    b1_t_setter::set(b1, 1);
    
    using b2_t_setter = type_2_setter<b2_t, group_tlist>::setter;
    b2_t_setter::set(b2, 2);

//    using c1_t_setter = type_2_setter<c1_t, group_tlist>::setter;
//    c1_t_setter::set(c1, 2);

    using type = tlist_find_if<bind<std::is_same, a1_t>::type, x_group >::result; // , x_group>;
    static_assert(std::is_same_v<a1_t, type>);
    static_assert(std::is_same_v<a1_t, tlist_find_if<bind<std::is_same, a1_t>::type, x_group>::result>);
}
c++
  • 1 个回答
  • 10 Views
Martin Hope
OlegUP
Asked: 2022-06-10 23:44:13 +0000 UTC

GNU Make:模式规则仅在第一个文件上触发

  • 1

有一个Makefile:

ROOT_DIR = $(PWD)
BUILD_DIR = $(ROOT_DIR)/build
CXX = g++ 

SOURCES = $(wildcard $(ROOT_DIR)/*.cc)
OBJS = $(patsubst %.cc, %.o, $(subst $(ROOT_DIR), $(BUILD_DIR), $(SOURCES)))

$(OBJS) : $(SOURCES)
    @echo $< $@
    $(CXX) -c -o $@ $<

all: $(OBJS)
    $(CXX) $? $<

clean:
    rm $(BUILD_DIR)/*o

print : $(ROOT_DIR)/*.cc
    @echo "OBJS" $(OBJS)
    @echo "SOURCES" $(SOURCES)
    @echo "@" $@
    @echo "<" $<
    @echo "^" $^
    @echo "*" $*

以及来源所在的目录:

build  main.cc  Makefile  smb1.cc  smb1.h  smb1_tr.cc

调用规则时,$(OBJS) : $(SOURCES)它仅适用于列表中的第一个文件。

$(OBJS) : %.cc 如果你写了没有组装先决条件的规则,它也根本不起作用。(例如,尝试将打印规则中的字符替换为。*)%

我究竟做错了什么?

GNU Make 4.1

您可以在此处下载整个目录:https ://disk.yandex.ru/d/r-wId25734YCwA

makefile
  • 1 个回答
  • 10 Views
Martin Hope
OlegUP
Asked: 2022-05-07 04:05:36 +0000 UTC

正则表达式从一组相同的字符串中选择一个字符串(不带前缀的字符串)

  • 1

假设有一个输入字符串数组:

s = ["NT LANMAN", "LANMAN", "DOS LANMAN", PC NETWORK PROGRAM""]

有必要使用正则表达式选择包含单词"LANMAN"但不包含短语的所有行"NT LANMAN"

我已经尝试过:

(?!NT\s*)LANMAN- 不适合。

[^Nn][^Tt]\s*LANMAN- 也。

(?!NT\s*LANMAN\s*)LANMAN- 也。

你可以在这里测试:https ://regex101.com/r/B5zf9O/1

检查器在这里:https ://wandbox.org/permlink/gwi7TwRtMVEiRmFV

регулярные-выражения
  • 2 个回答
  • 10 Views
Martin Hope
OlegUP
Asked: 2022-03-25 17:19:59 +0000 UTC

C++:编译器不在模板中内联函数

  • 1

将函数替换为模板时,编译器由于某种原因看不到它。虽然他看到了上面的两条线。

#include <iostream>
#include <iomanip>
#include <string>
#include <endian.h>

using namespace std;

template <typename RESULT_T, typename letoh_func, typename T> 
static RESULT_T compose_high_low(T high, T low)
{
    RESULT_T res = letoh_func(high);
    res <<= sizeof(T) * 8;
    res |= letoh_func(low);
    return res;
}

int main()
{
    uint16_t k = le16toh(1);
    uint16_t high = 1, low = 2;
    uint32_t n = compose_high_low<uint32_t, le16toh>(high, low); // error: 'le16toh' was not declared in this scope
    cout << hex << n << endl;
    return 0;
}

在这里运行:http: //cpp.sh/7w7afl

c++
  • 1 个回答
  • 10 Views
Martin Hope
OlegUP
Asked: 2021-12-02 23:50:20 +0000 UTC

构造函数的声明隐藏参数

  • 0

有一个小的嵌套结构:

struct decode_bunch_ctrl_t
{
    pure_smb2_header::flags_t   request_flags;
    bool                        flags_set;
    uint64_t                    msg_id;

    decode_bunch_ctrl_t(pure_smb2_header::flags_t request_flags_, uint64_t msg_id_) 
        : request_flags(request_flags_), flags_set(true), msg_id(msg_id_) {}
    decode_bunch_ctrl_t()
        : request_flags(pure_smb2_header::flags_t()), flags_set(false), msg_id(0) {}
    operator bool () { return flags_set && msg_id; }
};

结构pure_smb2_header::flags_t定义如下:

struct flags_t
{
    static const uint8_t PRIORITY_MASK = 0b111;

    uint32_t    reserved1:4;
    uint32_t    dfs_operations:1;
    uint32_t    replay_operation:1;
    uint32_t    reserved2:2;

    uint32_t    reserved3:16;

    uint32_t    server_to_redir:1;
    uint32_t    async_command:1;
    uint32_t    related_operations:1;
    uint32_t    signed_packet:1;
    uint32_t    priority_mask:3;
    uint32_t    reserved4:1;
}

使用不带参数的构造函数初始化对象时,会产生编译器错误:

error: declaration of ‘smb2_subdecoder::decode_bunch_ctrl_t dctrl_bunch’ shadows a parameter
  917 |     decode_bunch_ctrl_t dctrl_bunch;

但是在创建对象时没有参数被着色。问题是什么?

编译器版本:

gcc version 9.2.1 20191102 (Ubuntu 9.2.1-17ubuntu1~18.04.1)
c++
  • 1 个回答
  • 10 Views
Martin Hope
OlegUP
Asked: 2020-09-18 00:48:30 +0000 UTC

访问者模式和 OOP 规则

  • 1

让有一个类:

class A
{
private:
    int x;
    ...
};

Container<A>还有一些带有方法的容器Container<A>::accept<VisitorT>(const VisitorT visitor)

假设有一个特定的访问者,您需要在其中设置 A 类的字段。

void concrete_visitor(A& a)
{
   a.x = 1;
}

但是字段 x 是私有的。如果把它公之于众,那会不会违反OOP?如果从 Object Thinking 书的角度来看,setter 和 getter 也是违反封装的。

如何在不违反 OOP 原则的情况下正确协调这种行为?同时,不使用朋友,因为访问者可以是一个 lambda,并且可以有很多。

c++
  • 1 个回答
  • 10 Views
Martin Hope
OlegUP
Asked: 2020-09-25 19:38:30 +0000 UTC

Makefile 没有找到给定的目标

  • 0

我正在编写一个 HashMap 实现。Makefile 是手工编写的。

SHELL       := /bin/bash
PROJ_ROOT   = $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
MAKEFILE        = Makefile
DEL_FILE        = rm -f
SRC_DIR     = $(PROJ_ROOT)src
CITYHASH_SRC_DIR = $(SRC_DIR)/cityhash
TESTS_SRC_DIR   = $(PROJ_ROOT)test
BUILD_DIR   = $(PROJ_ROOT)build
LIB_BUILD_DIR   = $(BUILD_DIR)/lib
TESTS_BUILD_DIR = $(PROJ_ROOT)build/test

LIB_SOURCES = $(wildcard $(SRC_DIR)/*.cpp)
LIB_OBJS    = $(patsubst $(SRC_DIR)/%.cpp, $(LIB_BUILD_DIR)/.cpp.o%, $(LIB_SOURCES))
LIB     = $(LIB_BUILD_DIR)/libhashmap.so

TEST_SOURCES    = $(wildcard $(TESTS_SRC_DIR)/*.cpp)
TESTS_OBJS  = $(patsubst $(TESTS_SRC_DIR)/%.cpp, $(TESTS_BUILD_DIR)/.cpp.o%, $(TEST_SOURCES))
TESTS       = $(patsubst $(TESTS_SRC_DIR)/%.cpp, $(TESTS_BUILD_DIR)/%, $(TEST_SOURCES))

CITYHASH_OBJ    = $(TESTS_BUILD_DIR)/city.cc.o

CXX     = g++
INCLUDE_DIRS    = $(SRC_DIR)
CXX_FLAGS   = -std=c++14 -I$(INCLUDE_DIRS) -Wall -g -O3
CXX_LFLAGS  = -std=c++14 -Wall -g -O3 -L$(LIB_BUILD_DIR)

lib: $(LIB)

tests: tests_clean $(CITYHASH_OBJ) $(LIB) $(TESTS)

#$(CITYHASH_OBJ):
$(TESTS_BUILD_DIR)/city.cc.o: $(CITYHASH_SRC_DIR)/city.cc
    $(CXX) -c $(CXX_FLAGS) -o $@ $<

#$(LIB):  # во время выполнения зависимости этой цели...
$(LIB_BUILD_DIR)/libhashmap.so: $(LIB_BUILD_DIR)/%.cpp.o
    $(CXX) -shared $(CXX_LFLAGS) -o $@ $^

#$(LIB_OBJS): # Не может обнаружить эту цель
$(LIB_BUILD_DIR)/%.cpp.o: $(SRC_DIR)/%.cpp
    $(CXX) -c $(CXX_FLAGS) -o $@ $<

#$(TESTS_OBJS):
$(TESTS_BUILD_DIR)/%.cpp.o: $(TESTS_SRC_DIR)/%.cpp
    $(CXX) -c $(CXX_FLAGS) -o $@ $<

#$(TESTS):  
$(TESTS_BUILD_DIR)/%: $(TESTS_BUILD_DIR)/%.cpp.o $(TESTS_BUILD_DIR)/city.cc.o
    $(CXX) $(CXX_FLAGS) -o $@ $^
    @echo 'run a test: ' $@ 
    @$@
    @if [[ $$? == 0 ]]; then \
        echo "Test: '`basename $@`' Completed Succesfully!"; \
    else \
        echo "Test: '`basename $@`' Failed!"; \
    fi

tests_clean:
    $(DEL_FILE) $(TESTS_BUILD_DIR)/*

clean: tests_clean
    $(DEL_FILE) $(LIB_BUILD_DIR)/*

watch:
    $(foreach v, $(.VARIABLES), $(info $(v) = $($(v))))

当我尝试运行make tests make 时,它​​会引发错误:

make: *** Нет правила для сборки цели «cpp_hashmap/build/lib/%.cpp.o»,  
требуемой для «cpp_hashmap/build/lib/libhashmap.so».  Останов.

然而,目标被指出,检查错别字,一切都匹配。我什至不知道用什么方法挖。

您可以自己查看,链接到 github 存储库。在构建之前,您需要执行: mkdir -p build/{test,lib} 在项目的根目录中

makefile
  • 1 个回答
  • 10 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5