Variadic template function not working

Hi, I want to make this function to check if all the arguments are true but this doesn't work and from compiler warning I can really understand the problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

constexpr bool All() { return true; }

template<typename... Args>
constexpr bool All(bool b, Args...)
{
	return b && All(static_cast<bool>(Args)...);
}

int main()
{
	bool b = All(true, true, true);

	std::cout << "All true = " << b  << std::endl;

	system("pause");
}

Here are the warnings
In function 'constexpr bool All(bool, Args ...)':
8:40: error: expected primary-expression before ')' token
In instantiation of 'constexpr bool All(bool, Args ...) [with Args = {bool, bool}]':
13:31: required from here
9:1: error: body of constexpr function 'constexpr bool All(bool, Args ...) [with Args = {bool, bool}]' not a return-statement
9:1: warning: no return statement in function returning non-void [-Wreturn-type]
Figured it out! :)
had to use it like this
1
2
3
4
5
template<typename... Args>
constexpr bool All(bool b, Args... args)
{
	return b && All(static_cast<bool>(args)...);
}

Fold expression (C++17): http://en.cppreference.com/w/cpp/language/fold

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <cstring>

template< typename... ARGS > constexpr bool all( ARGS&&... args )
{ return ( ... && args ) ; } // unary left fold (C++17)

int main()
{
    std::cout << std::boolalpha << all( true, 5.6, std::cout, std::strlen ) << '\n' ; // true
}

http://coliru.stacked-crooked.com/a/d024f70b5ca3fb21
This is so cool, tnx man! Can't use it in my VS but looking forward to use all the new stuff that c++17 is going to offer :)

In the same time makes me sad that Scott Meyers is probably not going to write nice book about all the cool new stuff.
Visual Studio 2015:
Compile with the Clang 3.7 with Microsoft CodeGen (v140_clang_3_7) tool-chain;
specify -std=c++1z in Project => Properties => C/C++ => Command Line => Additional options.
Tnx man for the information but now I'm not that good with installing and using different compilers unless its just
sudo apt-get install ...
:D. It would take like a day or more for me now talking from experience.

For now will just stay with my current compiler (don't really know what it is) but when I'll finish with my (old pre c++17) books I'll have to install something better anyway.
Last edited on
Topic archived. No new replies allowed.