Templates and concepts

closed account (26q2b7Xj)
I am reading through a C++ book and one of the exercises that builds on top of the previous ones says the following: " Modify mode to accept an Integer concept. Verify that mode fails to instantiate with floating types like double".
Now, I am still new with concepts and it seems that VS won't recognize the concept keyword. In the meantime, I stuck with using static_assert. Can anyone show how I can do it with concepts?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <algorithm>
#include <type_traits>
#include <stdexcept>

template <typename T>
int mode(const T* values, std::size_t length)
{
	static_assert(std::is_integral<T>(), "Type must be an integer.");

	if (length <= 0)
		return 0;

	if (!values)
		throw std::invalid_argument{ "Invalid argument passed." };

	T number{ values[0] };
	T mode{ number };
	int count{ 1 };
	int count_mode{ 1 };

	for (std::size_t i{ 1 }; i < length; ++i)
	{
		if (values[i] == number)
			++count;
		else
		{
			if (count > count_mode)
			{
				count_mode = count;
				mode = number;
			}

			count = 1;
			number = values[i];
		}
	}

	return mode;
}

int main()
{
    int values[]{ 2, 1, 1, 5, 7, 1, 2, 3, 3, 1};
	std::size_t length{ std::size(values) };

	std::sort(std::begin(values), std::end(values));

	try
	{
		auto result{ mode(values, length) };

		// If exception is thrown, this won't execute.
		std::cout << "mode: " << result << '\n';
	}
	catch (const std::invalid_argument& e)
	{
		std::cerr << "Standard exception: " << e.what() << '\n';
	}
	catch (...)
	{
		std::cerr << "Unknown Exception caught.\n";
	}
}
What version of Visual Studio, and how are you compiling?

https://devblogs.microsoft.com/cppblog/c20-concepts-are-here-in-visual-studio-2019-version-16-3/
C++20 (which is what has concepts) is feature complete, but I don't think it's technically released yet as the official standard, so you probably have to set the compiler flag /std:c++latest .

https://docs.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance?view=vs-2019#note_20
Last edited on
closed account (26q2b7Xj)
According to C++ Language Standard in proprieties, I have it marked as, "Preview - Features from the Latest C++."
So, does this compile for you?

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

template <class T>
concept Integral = std::is_integral<T>::value;

template <Integral T>
void mode(T integral)
{
	std::cout << integral << "!\n";
}

int main()
{
	mode(42);
	//mode(4.2); // error
}


You didn't specifically mention which version of Visual Studio you are using.
What does Help --> About Microsoft Visual Studio say as the Version?
Last edited on
closed account (26q2b7Xj)
Version 16.7.1

I am getting Identifier "concept" is Undefined. Which also marks Integral with, "argument list with variable template Integral missing".
Hmm, that should be recent enough. I can't really say what the issue is.
The errors are actual compiler errors after clicking "Build Solution" (Ctrl + Shift + B), and not IntelliSense errors, right?

One last idea: Go to Project --> Properties. Then Configuration Properties --> C/C++ --> Command Line.
Copy and paste the text there so we can see it.

For example, mine is
/JMC /permissive- /GS /analyze- /W3 /Zc:wchar_t /ZI /Gm- /Od /sdl /Fd"Debug\vc142.pdb" /Zc:inline /fp:precise /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /Oy- /MDd /std:c++latest /FC /Fa"Debug\" /EHsc /nologo /Fo"Debug\" /Fp"Debug\ConceptsTest.pch" /diagnostics:column
Last edited on
closed account (26q2b7Xj)
/JMC /permissive- /GS /W4 /Zc:wchar_t /ZI /Gm- /Od /sdl /Fd"x64\Debug\vc142.pdb" /Zc:inline /fp:precise /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX /Zc:forScope /RTC1 /Gd /P /MDd /std:c++latest /FC /Fa"x64\Debug\" /EHsc /nologo /Fo"x64\Debug\" /Fp"x64\Debug\playground.pch" /diagnostics:column
Thanks, unfortunately I'm out of ideas.
Maybe someone else here will respond, but if not I'd try a Microsoft forum or something like that, and show the minimal example.

EDIT: I see you have /P as a flag
"The /P option suppresses compilation. It does not produce an .obj file, even if you use /Fo (Object File Name). You must resubmit the preprocessed file for compilation. /P also suppresses the output files from the /FA, /Fa, and /Fm options. For more information, see /FA, /Fa (Listing File) and /Fm (Name Mapfile)."

Try turning off "preprocess to a file"
https://docs.microsoft.com/en-us/cpp/build/reference/p-preprocess-to-a-file?view=vs-2019
Last edited on
Topic archived. No new replies allowed.