Skip to content

Commit fc70113

Browse files
WalterBrightdlang-bot
authored andcommitted
improve C-style Variadic Functions section
1 parent dc623d1 commit fc70113

File tree

1 file changed

+53
-40
lines changed

1 file changed

+53
-40
lines changed

spec/function.dd

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,67 +1776,80 @@ See also: $(GLINK2_ALTTEXT attribute, UserDefinedAttribute, User-Defined Attribu
17761776

17771777
$(H3 $(LNAME2 variadic, Variadic Functions))
17781778

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:)
17821781

17831782
$(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))
17871786
)
17881787

17891788

1790-
$(H4 $(LNAME2 c_style_cariadic_functions, C-style Variadic Functions))
1789+
$(H4 $(LNAME2 c_style_variadic_functions, C-style Variadic Functions))
17911790

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)).)
17951794

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+
)
17981799

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+
---
18031812

18041813
$(P There must be at least one non-variadic parameter declared.)
18051814

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+
---
18091818

18101819
$(P
18111820
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
18131822
functions like $(D printf).
1814-
)
1815-
1816-
$(P C-style variadic functions cannot be marked as $(D @safe).)
1823+
)
18171824

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)*, ...);
18211827

1822-
------
1823-
import core.stdc.stdarg;
1828+
void main()
1829+
{
1830+
printf("hello world\n");
1831+
}
1832+
---
18241833

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).)
18291835

1830-
void foo(int x, int y, ...)
1831-
{
1832-
va_list args;
18331836

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+
}
18351842

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+
---
18401853

18411854

18421855
$(H4 $(LNAME2 d_style_variadic_functions, D-style Variadic Functions))

0 commit comments

Comments
 (0)