主頁 > 後端開發 > 成員指標與mem_fn

成員指標與mem_fn

2020-09-14 15:24:46 後端開發

本文是<functional>系列的第4篇,

成員指標是一個非常具有C++特色的功能,更低級的語言(如C)沒有類,也就沒有成員的概念;更高級的語言(如Java)沒有指標,即使有也不會有成員指標這么拗口的東西,

上回在Stack Overflow上看到一個問題,C++是否允許delegate = object.method這種寫法,我猜他是從C#過來的,在C++中,這種寫法在語法上是不可能的,語意上可以用std::bind來實作,而本文的主題std::mem_fn,則是實作了delegate = method的功能,object插到了原來的引數串列的前面,成為新的函式物件的第一個引數,

成員指標

先說說成員指標,成員指標,分為物件成員指標與成員函式指標,下面的程式演示了如何定義和使用它們:

struct Test
{
    int object;
    void function(int) { }
};

int main()
{
    Test test;
    Test* ptr = &test;
    int Test::* po = &Test::object;
    test.*po;
    ptr->*po;
    void (Test::*pf)(int) = &Test::function;
    (test.*pf)(0);
    (ptr->*pf)(0);
}

定義為static的物件或函式,就好像它所在的類不存在一樣,只能用普通的指標與函式指標,

這一節的重點在于成員指標的模板匹配,首先,形如物件成員指標的型別可以匹配成員函式指標:

template<typename>
struct member_test;

template<typename Res, typename Class>
struct member_test<Res Class::*>
{
    using result_type = Res;
    using class_type = Class;
};
struct Test
{
    int object;
    void function(int) { }
};

using ObjectType = decltype(&Test::object);
using FunctionType = decltype(&Test::function);

static_assert(std::is_same<
    typename member_test<ObjectType>::result_type,
    int>::value, "");
static_assert(std::is_same<
    typename member_test<ObjectType>::class_type,
    Test>::value, "");
static_assert(std::is_same<
    typename member_test<FunctionType>::result_type,
    void(int)>::value, "");
static_assert(std::is_same<
    typename member_test<FunctionType>::class_type,
    Test>::value, "");

ObjectType可以匹配Res Class::*,其中ResintClassTest,這完全符合預期,令人震驚的是,FunctionType也可以匹配Res Class::*!其中Class依然為Test,而Res為函式型別void(int)

那么是否可以寫一個類模板,只能匹配成員函式指標而無法匹配物件成員指標呢?在此之前,為了能夠更有說服力地用static_assert表示一個類沒有result_type成員型別(而不是在編譯錯誤后把代碼注釋掉),我寫了個has_result_type型別,用的是昨天剛寫過的void_t技巧:

template<typename T, typename = void>
struct has_result_type
    : std::false_type { };

template<typename T>
struct has_result_type<T, std::void_t<typename T::result_type>>
    : std::true_type { };

只匹配成員函式指標,需要加上一個可變引數:

template<typename>
struct member_function_test;

template<typename Res, typename Class, typename... Args>
struct member_function_test<Res (Class::*)(Args...)>
{
    using result_type = Res;
    using class_type = Class;
};

static_assert(!has_result_type<
    member_function_test<ObjectType>>::value, "");
static_assert(has_result_type<
    member_function_test<FunctionType>>::value, "");
static_assert(std::is_same<
    typename member_function_test<FunctionType>::result_type,
    void>::value, "");

那么只匹配物件成員指標呢?很簡單,只需寫一個全部匹配的,再去掉成員函式指標即可:

template<typename>
struct member_object_test;

template<typename Res, typename Class>
struct member_object_test<Res Class::*>
{
    using result_type = Res;
    using class_type = Class;
};

template<typename Res, typename Class, typename... Args>
struct member_object_test<Res (Class::*)(Args...)> { };

static_assert(has_result_type<
    member_object_test<ObjectType>>::value, "");
static_assert(!has_result_type<
    member_object_test<FunctionType>>::value, "");
static_assert(std::is_same<
    typename member_object_test<ObjectType>::result_type,
    int>::value, "");

如果成員函式有const&會怎樣?

struct Test
{
    int object;
    void function(int) { }
    void function_const(int) const { }
    void function_ref(int) & { }
};

static_assert(std::is_same<
    typename member_test<decltype(&Test::function_const)>::result_type,
    void(int) const>::value, "");
static_assert(std::is_same<
    typename member_test<decltype(&Test::function_const)>::class_type,
    Test>::value, "");
static_assert(std::is_same<
    typename member_test<decltype(&Test::function_ref)>::result_type,
    void(int) &>::value, "");
static_assert(std::is_same<
    typename member_test<decltype(&Test::function_ref)>::class_type,
    Test>::value, "");

Res Class::*中的Class還是不變,但是Res變成了后加const&的函式型別,關于這兩個型別我沒有查到相關資料,只知道它們的std::is_function_vtrue,不過這就夠了,

mem_fn

懶得寫了,照搬cppreference上的代碼:

#include <functional>
#include <iostream>
 
struct Foo {
    void display_greeting() {
        std::cout << "Hello, world.\n";
    }
    void display_number(int i) {
        std::cout << "number: " << i << '\n';
    }
    int data = 7;
};
 
int main() {
    Foo f;
 
    auto greet = std::mem_fn(&Foo::display_greeting);
    greet(f);
 
    auto print_num = std::mem_fn(&Foo::display_number);
    print_num(f, 42);
 
    auto access_data = std::mem_fn(&Foo::data);
    std::cout << "data: " << access_data(f) << '\n';
}

輸出:

Hello, world.
number: 42
data: 7

我尋思著你能讀到這兒也不用我介紹std::mem_fn了吧,我的心思在它的實作上,

順便提醒,不要跟std::mem_fun搞混,那玩意兒是C++98的化石,

實作

std::mem_fn基于std::invokestd::invoke又基于std::result_of,所以從std::result_of講起,

SFINAE

在C++中,檢查一句陳述句是否合法有三種方式:目測、看編譯器給不給error、SFINAE,對于模板代碼,Visual Studio都智能不起來,更別說目測了;我們又不想看到編譯器的error,所以得學習SFINAE,Substitution Failure Is Not An Error,替換失敗不是錯誤,

struct __result_of_other_impl
{
  template<typename _Fn, typename... _Args>
    static __result_of_success<decltype(
    std::declval<_Fn>()(std::declval<_Args>()...)
    ), __invoke_other> _S_test(int);

  template<typename...>
    static __failure_type _S_test(...);
};

template<typename _Functor, typename... _ArgTypes>
  struct __result_of_impl<false, false, _Functor, _ArgTypes...>
  : private __result_of_other_impl
  {
    typedef decltype(_S_test<_Functor, _ArgTypes...>(0)) type;
  };

__result_of_other_impl里有兩個多載函式_S_test__result_of_impl通過decltype獲得它的回傳型別,當_Functor(_ArgTypes...)陳述句合法時,第一個_S_test安好,int優于...,多載決議為第一個,type定義為_S_test前面一長串;不合法時,第一個_S_test實體化失敗,但是模板替換失敗不是錯誤,編譯器繼續尋找正確的多載,找到第二個_S_test,它的變參模板和可變引數像黑洞一樣吞噬一切呼叫,一定能匹配上,type定義為__failure_type

后文中凡是出現_S_test的地方都使用了SFINAE的技巧,

result_of

// For several sfinae-friendly trait implementations we transport both the
// result information (as the member type) and the failure information (no
// member type). This is very similar to std::enable_if, but we cannot use
// them, because we need to derive from them as an implementation detail.

template<typename _Tp>
struct __success_type
{ typedef _Tp type; };

struct __failure_type
{ };

/// result_of
template<typename _Signature>
  class result_of;

// Sfinae-friendly result_of implementation:

#define __cpp_lib_result_of_sfinae 201210

struct __invoke_memfun_ref { };
struct __invoke_memfun_deref { };
struct __invoke_memobj_ref { };
struct __invoke_memobj_deref { };
struct __invoke_other { };

// Associate a tag type with a specialization of __success_type.
template<typename _Tp, typename _Tag>
  struct __result_of_success : __success_type<_Tp>
  { using __invoke_type = _Tag; };

// [func.require] paragraph 1 bullet 1:
struct __result_of_memfun_ref_impl
{
  template<typename _Fp, typename _Tp1, typename... _Args>
    static __result_of_success<decltype(
    (std::declval<_Tp1>().*std::declval<_Fp>())(std::declval<_Args>()...)
    ), __invoke_memfun_ref> _S_test(int);

  template<typename...>
    static __failure_type _S_test(...);
};

template<typename _MemPtr, typename _Arg, typename... _Args>
  struct __result_of_memfun_ref
  : private __result_of_memfun_ref_impl
  {
    typedef decltype(_S_test<_MemPtr, _Arg, _Args...>(0)) type;
  };

// [func.require] paragraph 1 bullet 2:
struct __result_of_memfun_deref_impl
{
  template<typename _Fp, typename _Tp1, typename... _Args>
    static __result_of_success<decltype(
    ((*std::declval<_Tp1>()).*std::declval<_Fp>())(std::declval<_Args>()...)
    ), __invoke_memfun_deref> _S_test(int);

  template<typename...>
    static __failure_type _S_test(...);
};

template<typename _MemPtr, typename _Arg, typename... _Args>
  struct __result_of_memfun_deref
  : private __result_of_memfun_deref_impl
  {
    typedef decltype(_S_test<_MemPtr, _Arg, _Args...>(0)) type;
  };

// [func.require] paragraph 1 bullet 3:
struct __result_of_memobj_ref_impl
{
  template<typename _Fp, typename _Tp1>
    static __result_of_success<decltype(
    std::declval<_Tp1>().*std::declval<_Fp>()
    ), __invoke_memobj_ref> _S_test(int);

  template<typename, typename>
    static __failure_type _S_test(...);
};

template<typename _MemPtr, typename _Arg>
  struct __result_of_memobj_ref
  : private __result_of_memobj_ref_impl
  {
    typedef decltype(_S_test<_MemPtr, _Arg>(0)) type;
  };

// [func.require] paragraph 1 bullet 4:
struct __result_of_memobj_deref_impl
{
  template<typename _Fp, typename _Tp1>
    static __result_of_success<decltype(
    (*std::declval<_Tp1>()).*std::declval<_Fp>()
    ), __invoke_memobj_deref> _S_test(int);

  template<typename, typename>
    static __failure_type _S_test(...);
};

template<typename _MemPtr, typename _Arg>
  struct __result_of_memobj_deref
  : private __result_of_memobj_deref_impl
  {
    typedef decltype(_S_test<_MemPtr, _Arg>(0)) type;
  };

template<typename _MemPtr, typename _Arg>
  struct __result_of_memobj;

template<typename _Res, typename _Class, typename _Arg>
  struct __result_of_memobj<_Res _Class::*, _Arg>
  {
    typedef typename remove_cv<typename remove_reference<
      _Arg>::type>::type _Argval;
    typedef _Res _Class::* _MemPtr;
    typedef typename conditional<__or_<is_same<_Argval, _Class>,
      is_base_of<_Class, _Argval>>::value,
      __result_of_memobj_ref<_MemPtr, _Arg>,
      __result_of_memobj_deref<_MemPtr, _Arg>
    >::type::type type;
  };

template<typename _MemPtr, typename _Arg, typename... _Args>
  struct __result_of_memfun;

template<typename _Res, typename _Class, typename _Arg, typename... _Args>
  struct __result_of_memfun<_Res _Class::*, _Arg, _Args...>
  {
    typedef typename remove_cv<typename remove_reference<
      _Arg>::type>::type _Argval;
    typedef _Res _Class::* _MemPtr;
    typedef typename conditional<__or_<is_same<_Argval, _Class>,
      is_base_of<_Class, _Argval>>::value,
      __result_of_memfun_ref<_MemPtr, _Arg, _Args...>,
      __result_of_memfun_deref<_MemPtr, _Arg, _Args...>
    >::type::type type;
  };

// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 2219.  INVOKE-ing a pointer to member with a reference_wrapper
//        as the object expression

// Used by result_of, invoke etc. to unwrap a reference_wrapper.
template<typename _Tp, typename _Up = typename decay<_Tp>::type>
  struct __inv_unwrap
  {
    using type = _Tp;
  };

template<typename _Tp, typename _Up>
  struct __inv_unwrap<_Tp, reference_wrapper<_Up>>
  {
    using type = _Up&;
  };

template<bool, bool, typename _Functor, typename... _ArgTypes>
  struct __result_of_impl
  {
    typedef __failure_type type;
  };

template<typename _MemPtr, typename _Arg>
  struct __result_of_impl<true, false, _MemPtr, _Arg>
  : public __result_of_memobj<typename decay<_MemPtr>::type,
                              typename __inv_unwrap<_Arg>::type>
  { };

template<typename _MemPtr, typename _Arg, typename... _Args>
  struct __result_of_impl<false, true, _MemPtr, _Arg, _Args...>
  : public __result_of_memfun<typename decay<_MemPtr>::type,
                              typename __inv_unwrap<_Arg>::type, _Args...>
  { };

// [func.require] paragraph 1 bullet 5:
struct __result_of_other_impl
{
  template<typename _Fn, typename... _Args>
    static __result_of_success<decltype(
    std::declval<_Fn>()(std::declval<_Args>()...)
    ), __invoke_other> _S_test(int);

  template<typename...>
    static __failure_type _S_test(...);
};

template<typename _Functor, typename... _ArgTypes>
  struct __result_of_impl<false, false, _Functor, _ArgTypes...>
  : private __result_of_other_impl
  {
    typedef decltype(_S_test<_Functor, _ArgTypes...>(0)) type;
  };

// __invoke_result (std::invoke_result for C++11)
template<typename _Functor, typename... _ArgTypes>
  struct __invoke_result
  : public __result_of_impl<
      is_member_object_pointer<
        typename remove_reference<_Functor>::type
      >::value,
      is_member_function_pointer<
        typename remove_reference<_Functor>::type
      >::value,
      _Functor, _ArgTypes...
    >::type
  { };

template<typename _Functor, typename... _ArgTypes>
  struct result_of<_Functor(_ArgTypes...)>
  : public __invoke_result<_Functor, _ArgTypes...>
  { };

/// std::invoke_result
template<typename _Functor, typename... _ArgTypes>
  struct invoke_result
  : public __invoke_result<_Functor, _ArgTypes...>
  { };

std::result_ofstd::invoke_result本質上是相同的,無非是模板引數_Functor(_ArgTypes...)_Functor, _ArgTypes...的區別,前者在C++17中廢棄,后者在C++17中加入,

__invoke_result借助_Functor的型別分為三種情況:

  • __result_of_impl<false, false, _Functor, _ArgTypes...>,可呼叫物件型別不是成員指標,繼承__result_of_other_impl,后者在上一節介紹過了;

  • __result_of_impl<true, false, _MemPtr, _Arg>,可呼叫物件是物件成員指標,繼承__result_of_memobj

    • _Argval_Class相同或_Class_Argval的基類時(其實is_base_of就可以概括這種關系;子類成員可以呼叫基類成員指標),使用__result_of_memobj_ref,呼叫方式為.*

    • 否則,呼叫引數是個指標,使用__result_of_memobj_deref,呼叫方式為->*

  • __result_of_impl<false, true, _MemPtr, _Arg, _Args...>,可呼叫物件是成員函式指標,詳細討論與上一種情況類似,不再贅述,

總之,對于合法的呼叫型別,__invoke_result最后繼承到__success_type,定義type為回傳型別;否則繼承__failure_type,沒有type成員,

Tag Dispatching

你注意到了嗎?__result_of_success__success_type包裝了一下,加入了_Tag模板引數并定義為__invoke_type,在隨后的實體化中,__invoke_type都是以下5個型別之一:

struct __invoke_memfun_ref { };
struct __invoke_memfun_deref { };
struct __invoke_memobj_ref { };
struct __invoke_memobj_deref { };
struct __invoke_other { };

這些型別極大地簡化了__invoke的實作:

// Used by __invoke_impl instead of std::forward<_Tp> so that a
// reference_wrapper is converted to an lvalue-reference.
template<typename _Tp, typename _Up = typename __inv_unwrap<_Tp>::type>
  constexpr _Up&&
  __invfwd(typename remove_reference<_Tp>::type& __t) noexcept
  { return static_cast<_Up&&>(__t); }

template<typename _Res, typename _Fn, typename... _Args>
  constexpr _Res
  __invoke_impl(__invoke_other, _Fn&& __f, _Args&&... __args)
  { return std::forward<_Fn>(__f)(std::forward<_Args>(__args)...); }

template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
  constexpr _Res
  __invoke_impl(__invoke_memfun_ref, _MemFun&& __f, _Tp&& __t,
                _Args&&... __args)
  { return (__invfwd<_Tp>(__t).*__f)(std::forward<_Args>(__args)...); }

template<typename _Res, typename _MemFun, typename _Tp, typename... _Args>
  constexpr _Res
  __invoke_impl(__invoke_memfun_deref, _MemFun&& __f, _Tp&& __t,
                _Args&&... __args)
  {
    return ((*std::forward<_Tp>(__t)).*__f)(std::forward<_Args>(__args)...);
  }

template<typename _Res, typename _MemPtr, typename _Tp>
  constexpr _Res
  __invoke_impl(__invoke_memobj_ref, _MemPtr&& __f, _Tp&& __t)
  { return __invfwd<_Tp>(__t).*__f; }

template<typename _Res, typename _MemPtr, typename _Tp>
  constexpr _Res
  __invoke_impl(__invoke_memobj_deref, _MemPtr&& __f, _Tp&& __t)
  { return (*std::forward<_Tp>(__t)).*__f; }

/// Invoke a callable object.
template<typename _Callable, typename... _Args>
  constexpr typename __invoke_result<_Callable, _Args...>::type
  __invoke(_Callable&& __fn, _Args&&... __args)
  noexcept(__is_nothrow_invocable<_Callable, _Args...>::value)
  {
    using __result = __invoke_result<_Callable, _Args...>;
    using __type = typename __result::type;
    using __tag = typename __result::__invoke_type;
    return std::__invoke_impl<__type>(__tag{}, std::forward<_Callable>(__fn),
                                      std::forward<_Args>(__args)...);
  }

/// Invoke a callable object.
template<typename _Callable, typename... _Args>
  inline invoke_result_t<_Callable, _Args...>
  invoke(_Callable&& __fn, _Args&&... __args)
  noexcept(is_nothrow_invocable_v<_Callable, _Args...>)
  {
    return std::__invoke(std::forward<_Callable>(__fn),
                         std::forward<_Args>(__args)...);
  }

__invoke中定義這個__invoke_type__tag,然后呼叫__invoke_impl時把__tag的實體傳入,根據__tag的型別,編譯器將多載函式決議為5個__invoke_impl中對應的那個,

這種技巧稱為tag dispatching,我在std::function中也介紹過,

mem_fn

template<typename _MemFunPtr,
         bool __is_mem_fn = is_member_function_pointer<_MemFunPtr>::value>
  class _Mem_fn_base
  : public _Mem_fn_traits<_MemFunPtr>::__maybe_type
  {
    using _Traits = _Mem_fn_traits<_MemFunPtr>;

    using _Arity = typename _Traits::__arity;
    using _Varargs = typename _Traits::__vararg;

    template<typename _Func, typename... _BoundArgs>
      friend struct _Bind_check_arity;

    _MemFunPtr _M_pmf;

  public:

    using result_type = typename _Traits::__result_type;

    explicit constexpr
    _Mem_fn_base(_MemFunPtr __pmf) noexcept : _M_pmf(__pmf) { }

    template<typename... _Args>
      auto
      operator()(_Args&&... __args) const
      noexcept(noexcept(
            std::__invoke(_M_pmf, std::forward<_Args>(__args)...)))
      -> decltype(std::__invoke(_M_pmf, std::forward<_Args>(__args)...))
      { return std::__invoke(_M_pmf, std::forward<_Args>(__args)...); }
  };

template<typename _MemObjPtr>
  class _Mem_fn_base<_MemObjPtr, false>
  {
    using _Arity = integral_constant<size_t, 0>;
    using _Varargs = false_type;

    template<typename _Func, typename... _BoundArgs>
      friend struct _Bind_check_arity;

    _MemObjPtr _M_pm;

  public:
    explicit constexpr
    _Mem_fn_base(_MemObjPtr __pm) noexcept : _M_pm(__pm) { }

    template<typename _Tp>
      auto
      operator()(_Tp&& __obj) const
      noexcept(noexcept(std::__invoke(_M_pm, std::forward<_Tp>(__obj))))
      -> decltype(std::__invoke(_M_pm, std::forward<_Tp>(__obj)))
      { return std::__invoke(_M_pm, std::forward<_Tp>(__obj)); }
  };

template<typename _MemberPointer>
  struct _Mem_fn; // undefined

template<typename _Res, typename _Class>
  struct _Mem_fn<_Res _Class::*>
  : _Mem_fn_base<_Res _Class::*>
  {
    using _Mem_fn_base<_Res _Class::*>::_Mem_fn_base;
  };

template<typename _Tp, typename _Class>
  inline _Mem_fn<_Tp _Class::*>
  mem_fn(_Tp _Class::* __pm) noexcept
  {
    return _Mem_fn<_Tp _Class::*>(__pm);
  }

std::mem_fn回傳型別為_Mem_fn_Mem_fn繼承_Mem_fn_base,后者分物件成員指標與成員函式指標兩種情況,operator()都轉發引數呼叫__invoke

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/36256.html

標籤:C++

上一篇:C++基礎 學習筆記四:多載之函式多載

下一篇:學生資訊管理系統.cpp(大二上)

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Rust中的智能指標:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak

    Rust中的智能指標是什么 智能指標(smart pointers)是一類資料結構,是擁有資料所有權和額外功能的指標。是指標的進一步發展 指標(pointer)是一個包含記憶體地址的變數的通用概念。這個地址參考,或 ” 指向”(points at)一些其 他資料 。參考以 & 符號為標志并借用了他們所 ......

    uj5u.com 2023-04-20 07:24:10 more
  • Java的值傳遞和參考傳遞

    值傳遞不會改變本身,參考傳遞(如果傳遞的值需要實體化到堆里)如果發生修改了會改變本身。 1.基本資料型別都是值傳遞 package com.example.basic; public class Test { public static void main(String[] args) { int ......

    uj5u.com 2023-04-20 07:24:04 more
  • [2]SpinalHDL教程——Scala簡單入門

    第一個 Scala 程式 shell里面輸入 $ scala scala> 1 + 1 res0: Int = 2 scala> println("Hello World!") Hello World! 檔案形式 object HelloWorld { /* 這是我的第一個 Scala 程式 * 以 ......

    uj5u.com 2023-04-20 07:23:58 more
  • 理解函式指標和回呼函式

    理解 函式指標 指向函式的指標。比如: 理解函式指標的偽代碼 void (*p)(int type, char *data); // 定義一個函式指標p void func(int type, char *data); // 宣告一個函式func p = func; // 將指標p指向函式func ......

    uj5u.com 2023-04-20 07:23:52 more
  • Django筆記二十五之資料庫函式之日期函式

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十五之資料庫函式之日期函式 日期函式主要介紹兩個大類,Extract() 和 Trunc() Extract() 函式作用是提取日期,比如我們可以提取一個日期欄位的年份,月份,日等資料 Trunc() 的作用則是截取,比如 2022-0 ......

    uj5u.com 2023-04-20 07:23:45 more
  • 一天吃透JVM面試八股文

    什么是JVM? JVM,全稱Java Virtual Machine(Java虛擬機),是通過在實際的計算機上仿真模擬各種計算機功能來實作的。由一套位元組碼指令集、一組暫存器、一個堆疊、一個垃圾回收堆和一個存盤方法域等組成。JVM屏蔽了與作業系統平臺相關的資訊,使得Java程式只需要生成在Java虛擬機 ......

    uj5u.com 2023-04-20 07:23:31 more
  • 使用Java接入小程式訂閱訊息!

    更新完微信服務號的模板訊息之后,我又趕緊把微信小程式的訂閱訊息給實作了!之前我一直以為微信小程式也是要企業才能申請,沒想到小程式個人就能申請。 訊息推送平臺🔥推送下發【郵件】【短信】【微信服務號】【微信小程式】【企業微信】【釘釘】等訊息型別。 https://gitee.com/zhongfuch ......

    uj5u.com 2023-04-20 07:22:59 more
  • java -- 緩沖流、轉換流、序列化流

    緩沖流 緩沖流, 也叫高效流, 按照資料型別分類: 位元組緩沖流:BufferedInputStream,BufferedOutputStream 字符緩沖流:BufferedReader,BufferedWriter 緩沖流的基本原理,是在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖 ......

    uj5u.com 2023-04-20 07:22:49 more
  • Java-SpringBoot-Range請求頭設定實作視頻分段傳輸

    老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了 下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了 寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧. 業務場景:視頻分段傳輸、視頻多段傳輸(理 ......

    uj5u.com 2023-04-20 07:22:42 more
  • Windows 10開發教程_編程入門自學教程_菜鳥教程-免費教程分享

    教程簡介 Windows 10開發入門教程 - 從簡單的步驟了解Windows 10開發,從基本到高級概念,包括簡介,UWP,第一個應用程式,商店,XAML控制元件,資料系結,XAML性能,自適應設計,自適應UI,自適應代碼,檔案管理,SQLite資料庫,應用程式到應用程式通信,應用程式本地化,應用程式 ......

    uj5u.com 2023-04-20 07:22:35 more