@@ -1776,67 +1776,80 @@ See also: $(GLINK2_ALTTEXT attribute, UserDefinedAttribute, User-Defined Attribu
1776
1776
1777
1777
$(H3 $(LNAME2 variadic, Variadic Functions))
1778
1778
1779
- $(P Functions taking a variable number of arguments are called
1780
- variadic functions. A variadic function can take one of
1781
- three forms:)
1779
+ $(P $(I Variadic Functions) take a variable number of arguments.
1780
+ There are three forms:)
1782
1781
1783
1782
$(OL
1784
- $(LI C-style variadic functions)
1785
- $(LI Variadic functions with type info)
1786
- $(LI Typesafe variadic functions)
1783
+ $(LI $(RELATIVE_LINK2 c_style_variadic_functions, C-style variadic functions) )
1784
+ $(LI $(RELATIVE_LINK2 d_style_variadic_functions, Variadic functions with type info) )
1785
+ $(LI $(RELATIVE_LINK2 typesafe-variadic_functions, Typesafe variadic functions) )
1787
1786
)
1788
1787
1789
1788
1790
- $(H4 $(LNAME2 c_style_cariadic_functions , C-style Variadic Functions))
1789
+ $(H4 $(LNAME2 c_style_variadic_functions , C-style Variadic Functions))
1791
1790
1792
- $(P A C-style variadic function is declared as taking
1793
- a parameter of ... after the required function parameters .
1794
- It has non-D linkage, such as $(D extern (C)): )
1791
+ $(P A C-style variadic function is declared with
1792
+ a parameter ` ...` as the last function parameter .
1793
+ It has non-D linkage, such as $(D extern (C)). )
1795
1794
1796
- ------
1797
- extern (C) void foo(int x, int y, ...);
1795
+ $(P To access the variadic arguments,
1796
+ import the standard library
1797
+ module $(LINK2 $(ROOT_DIR)phobos/core_stdc_stdarg.html, $(D core.stdc.stdarg)).
1798
+ )
1798
1799
1799
- foo(3, 4); // ok
1800
- foo(3, 4, 6.8); // ok, one variadic argument
1801
- foo(2); // error, y is a required argument
1802
- ------
1800
+ ---
1801
+ import core.stdc.stdarg;
1802
+
1803
+ extern (C) void dry(int x, int y, ...); // C-style Variadic Function
1804
+
1805
+ void spin()
1806
+ {
1807
+ dry(3, 4); // ok, no variadic arguments
1808
+ dry(3, 4, 6.8); // ok, one variadic argument
1809
+ dry(2); // error, no argument for parameter y
1810
+ }
1811
+ ---
1803
1812
1804
1813
$(P There must be at least one non-variadic parameter declared.)
1805
1814
1806
- --- ---
1807
- extern (C) int def(...); // error, must have at least one parameter
1808
- --- ---
1815
+ ---
1816
+ extern (C) int def(...); // error, must have at least one parameter
1817
+ ---
1809
1818
1810
1819
$(P
1811
1820
C-style variadic functions match the C calling convention for
1812
- variadic functions, and is most useful for calling C library
1821
+ variadic functions, and can call C Standard library
1813
1822
functions like $(D printf).
1814
- )
1815
-
1816
- $(P C-style variadic functions cannot be marked as $(D @safe).)
1823
+ )
1817
1824
1818
- $(P Access to variadic arguments is done using the standard library
1819
- module $(D core.stdc.stdarg).
1820
- )
1825
+ ---
1826
+ extern (C) int printf(const(char)*, ...);
1821
1827
1822
- ------
1823
- import core.stdc.stdarg;
1828
+ void main()
1829
+ {
1830
+ printf("hello world\n");
1831
+ }
1832
+ ---
1824
1833
1825
- void test()
1826
- {
1827
- foo(3, 4, 5); // first variadic argument is 5
1828
- }
1834
+ $(P C-style variadic functions cannot be marked as $(D @safe).)
1829
1835
1830
- void foo(int x, int y, ...)
1831
- {
1832
- va_list args;
1833
1836
1834
- va_start(args, y); // y is the last named parameter
1837
+ ---
1838
+ void wash()
1839
+ {
1840
+ rinse(3, 4, 5); // first variadic argument is 5
1841
+ }
1835
1842
1836
- int z;
1837
- va_arg(args, z); // z is set to 5
1838
- }
1839
- ------
1843
+ import core.stdc.stdarg;
1844
+ extern (C) void rinse(int x, int y, ...)
1845
+ {
1846
+ va_list args;
1847
+ va_start(args, y); // y is the last named parameter
1848
+ int z;
1849
+ va_arg(args, z); // z is set to 5
1850
+ va_end(args);
1851
+ }
1852
+ ---
1840
1853
1841
1854
1842
1855
$(H4 $(LNAME2 d_style_variadic_functions, D-style Variadic Functions))
0 commit comments