Coder Social home page Coder Social logo

Comments (4)

v1nh1shungry avatar v1nh1shungry commented on May 19, 2024 2

首先回答(...)variadic function,大概就是可以接受任意类型的任意数量的参数,详情见https://en.cppreference.com/w/cpp/utility/variadic。
然后不知道你清不清楚为什么有这么一坨东西,我稍微讲一下,也顺便让大家看看我说得对不对。

  struct two { char a; char b; };
  template <class U> static two test(...);
  template <class U> static char test(typename U::iterator_category* = 0);

这里利用了SFINAE,作用是用来检测T这个类型有没有iterator_category这个成员类型别名,如果有,就会匹配到template <class U> static char test(typename U::iterator_category* = 0)然后返回类型是char,否则匹配到template <class U> static two test(...)返回类型是two,注意到chartwo的大小是不一样的。所以看到

static const bool value = sizeof(test<T>(0)) == sizeof(char);

就可以轻松靠判断test返回类型的大小是否等于sizeof(char)来判断T有没有iterator_category这个成员了,功能正好这就是类型名has_iterator_cat描述的。以前没有concept的时候就只能靠这样嗯造。test没有定义是正常的,因为sizeof运算符不会真的运行这个函数,只会推导出函数返回类型,说白了这个函数就纯纯工具人,只是用来获取类型而已。SFINAE详情看https://en.cppreference.com/w/cpp/language/sfinae。
如果有哪里说得不对的还请指正。

from mytinystl.

flysky1241 avatar flysky1241 commented on May 19, 2024

其实 这个 ... 这三个点是 八成是 任意类型的意思,但是不明白这个 test 模板函数 是没有 定义的。

from mytinystl.

flysky1241 avatar flysky1241 commented on May 19, 2024

我想知道 这个 test 函数模板 应该是有定义的,不是很懂

from mytinystl.

Alinshans avatar Alinshans commented on May 19, 2024

楼上对的

from mytinystl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.