Skip to content

Commit 85b39ac

Browse files
author
Colin Robertson
committed
Add error C7510
1 parent 97adff9 commit 85b39ac

File tree

5 files changed

+117
-8
lines changed

5 files changed

+117
-8
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
---
2+
description: "Learn about the causes of Compiler error C7510 and how to fix it."
3+
title: "Compiler Error C7510"
4+
ms.date: 04/21/2021
5+
f1_keywords: ["C7510"]
6+
helpviewer_keywords: ["C7510"]
7+
---
8+
# Compiler Error C7510
9+
10+
> '*type-name*': use of dependent template name must be prefixed with 'template'\
11+
> '*type-name*': use of dependent type name must be prefixed with 'typename'
12+
13+
In [`/permissive-`](../../build/reference/permissive-standards-conformance.md) mode, the compiler requires the **`template`** keyword to precede a template name when it comes after a dependent [`nested-name-specifier`](../../cpp/scope-resolution-operator.md). Similar rules hold for types qualified by **`typename`**.
14+
15+
## Remarks
16+
17+
Compiler behavior has changed starting in Visual Studio 2017 version 15.8 under [`/permissive-`](../../build/reference/permissive-standards-conformance.md) mode. The compiler requires the **`template`** or **`typename`** keyword to precede a template or type name when it comes after a dependent *`nested-name-specifier`*. For more information, see [Name resolution for dependent types](../../cpp/name-resolution-for-dependent-types.md) and [Templates and name resolution](../../cpp/templates-and-name-resolution.md).
18+
19+
## Examples
20+
21+
The following code under [`/permissive-`](../../build/reference/permissive-standards-conformance.md) mode now raises C7510:
22+
23+
```cpp
24+
template<typename T> struct Base
25+
{
26+
template<class U> void example() {}
27+
};
28+
29+
template<typename T>
30+
struct X : Base<T>
31+
{
32+
void example()
33+
{
34+
Base<T>::example<int>(); // C7510: 'example': use of dependent
35+
// template name must be prefixed with 'template'
36+
// note: see reference to class template instantiation
37+
// 'X<T>' being compiled
38+
}
39+
};
40+
```
41+
42+
To fix the error, add the **`template`** keyword to the `Base<T>::example<int>();` statement, as shown in the following example:
43+
44+
```cpp
45+
template<typename T> struct Base
46+
{
47+
template<class U> void example() {}
48+
};
49+
50+
template<typename T>
51+
struct X : Base<T>
52+
{
53+
void example()
54+
{
55+
// Add template keyword here:
56+
Base<T>::template example<int>();
57+
}
58+
};
59+
```
60+
61+
In Visual Studio 2019 under **`/std:c++latest`**, template function bodies that have **`if constexpr`** statements have extra parsing-related checks enabled. For example, in Visual Studio 2017 the following code produces C7510 only if the **`/permissive-`** option is set. In Visual Studio 2019 the same code raises errors even when the **`/permissive`** option is set:
62+
63+
```cpp
64+
// C7510.cpp
65+
// compile using: cl /EHsc /W4 /permissive /std:c++latest C7510.cpp
66+
#include <iostream>
67+
68+
template <typename T>
69+
int f()
70+
{
71+
T::Type a; // error C7510: 'Type': use of dependent type name must be prefixed with 'typename'
72+
// To avoid the error, add the 'typename' keyword. Use this declaration instead:
73+
// typename T::Type a;
74+
75+
if constexpr (a.val)
76+
{
77+
return 1;
78+
}
79+
else
80+
{
81+
return 2;
82+
}
83+
}
84+
85+
struct X
86+
{
87+
using Type = X;
88+
constexpr static int val = 1;
89+
};
90+
91+
int main()
92+
{
93+
std::cout << f<X>() << "\n";
94+
}
95+
```
96+
97+
## See also
98+
99+
[`/permissive-` (Standards conformance)](../../build/reference/permissive-standards-conformance.md)\
100+
[Name resolution for dependent types](../../cpp/name-resolution-for-dependent-types.md)\
101+
[Templates and name resolution](../../cpp/templates-and-name-resolution.md)\
102+
[`typename`](../../cpp/typename.md)

docs/error-messages/compiler-errors-2/compiler-errors-c7500-through-c7999.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
description: "Learn more about: Compiler errors C7500 through C7999"
33
title: "Compiler errors C7500 through C7999"
44
ms.date: 04/18/2021
5-
f1_keywords: ["C7500", "C7501", "C7502", "C7503", "C7504", "C7505", "C7506", "C7507", "C7508", "C7509", "C7510", "C7511", "C7512", "C7513", "C7514", "C7515", "C7516", "C7517", "C7518", "C7519", "C7520", "C7521", "C7522", "C7523", "C7524", "C7525", "C7526", "C7527", "C7528", "C7529", "C7530", "C7531", "C7532", "C7533", "C7534", "C7535", "C7536", "C7537", "C7538", "C7539", "C7540", "C7541", "C7542", "C7543", "C7544", "C7545", "C7546", "C7547", "C7548", "C7549", "C7550", "C7551", "C7552", "C7553", "C7554", "C7555", "C7556", "C7557", "C7558", "C7559", "C7560", "C7561", "C7562", "C7563", "C7564", "C7565", "C7566", "C7567", "C7568", "C7569", "C7570", "C7571", "C7572", "C7573", "C7574", "C7575", "C7576", "C7577", "C7578", "C7579", "C7580", "C7581", "C7582", "C7583", "C7584", "C7585", "C7586", "C7587", "C7588", "C7589", "C7590", "C7591", "C7592", "C7593", "C7594", "C7595", "C7596", "C7597", "C7599", "C7600", "C7601", "C7602", "C7603", "C7604", "C7605", "C7606", "C7607", "C7608", "C7609", "C7610", "C7611", "C7612", "C7613", "C7614", "C7615", "C7616", "C7617", "C7618", "C7619", "C7620", "C7621", "C7622", "C7623", "C7624", "C7625", "C7627", "C7628", "C7629", "C7630", "C7631", "C7632", "C7633", "C7634", "C7635", "C7636", "C7637", "C7638", "C7639", "C7640", "C7641", "C7642", "C7643", "C7644", "C7645", "C7646", "C7647", "C7648", "C7649", "C7650", "C7651", "C7652", "C7653", "C7654", "C7655", "C7656", "C7657", "C7658", "C7659", "C7660", "C7661", "C7662", "C7700", "C7701", "C7702", "C7703", "C7704"]
6-
helpviewer_keywords: ["C7500", "C7501", "C7502", "C7503", "C7504", "C7505", "C7506", "C7507", "C7508", "C7509", "C7510", "C7511", "C7512", "C7513", "C7514", "C7515", "C7516", "C7517", "C7518", "C7519", "C7520", "C7521", "C7522", "C7523", "C7524", "C7525", "C7526", "C7527", "C7528", "C7529", "C7530", "C7531", "C7532", "C7533", "C7534", "C7535", "C7536", "C7537", "C7538", "C7539", "C7540", "C7541", "C7542", "C7543", "C7544", "C7545", "C7546", "C7547", "C7548", "C7549", "C7550", "C7551", "C7552", "C7553", "C7554", "C7555", "C7556", "C7557", "C7558", "C7559", "C7560", "C7561", "C7562", "C7563", "C7564", "C7565", "C7566", "C7567", "C7568", "C7569", "C7570", "C7571", "C7572", "C7573", "C7574", "C7575", "C7576", "C7577", "C7578", "C7579", "C7580", "C7581", "C7582", "C7583", "C7584", "C7585", "C7586", "C7587", "C7588", "C7589", "C7590", "C7591", "C7592", "C7593", "C7594", "C7595", "C7596", "C7597", "C7599", "C7600", "C7601", "C7602", "C7603", "C7604", "C7605", "C7606", "C7607", "C7608", "C7609", "C7610", "C7611", "C7612", "C7613", "C7614", "C7615", "C7616", "C7617", "C7618", "C7619", "C7620", "C7621", "C7622", "C7623", "C7624", "C7625", "C7627", "C7628", "C7629", "C7630", "C7631", "C7632", "C7633", "C7634", "C7635", "C7636", "C7637", "C7638", "C7639", "C7640", "C7641", "C7642", "C7643", "C7644", "C7645", "C7646", "C7647", "C7648", "C7649", "C7650", "C7651", "C7652", "C7653", "C7654", "C7655", "C7656", "C7657", "C7658", "C7659", "C7660", "C7661", "C7662", "C7700", "C7701", "C7702", "C7703", "C7704"]
5+
f1_keywords: ["C7500", "C7501", "C7502", "C7503", "C7504", "C7505", "C7506", "C7507", "C7508", "C7509", "C7511", "C7512", "C7513", "C7514", "C7515", "C7516", "C7517", "C7518", "C7519", "C7520", "C7521", "C7522", "C7523", "C7524", "C7525", "C7526", "C7527", "C7528", "C7529", "C7530", "C7531", "C7532", "C7533", "C7534", "C7535", "C7536", "C7537", "C7538", "C7539", "C7540", "C7541", "C7542", "C7543", "C7544", "C7545", "C7546", "C7547", "C7548", "C7549", "C7550", "C7551", "C7552", "C7553", "C7554", "C7555", "C7556", "C7557", "C7558", "C7559", "C7560", "C7561", "C7562", "C7563", "C7564", "C7565", "C7566", "C7567", "C7568", "C7569", "C7570", "C7571", "C7572", "C7573", "C7574", "C7575", "C7576", "C7577", "C7578", "C7579", "C7580", "C7581", "C7582", "C7583", "C7584", "C7585", "C7586", "C7587", "C7588", "C7589", "C7590", "C7591", "C7592", "C7593", "C7594", "C7595", "C7596", "C7597", "C7599", "C7600", "C7601", "C7602", "C7603", "C7604", "C7605", "C7606", "C7607", "C7608", "C7609", "C7610", "C7611", "C7612", "C7613", "C7614", "C7615", "C7616", "C7617", "C7618", "C7619", "C7620", "C7621", "C7622", "C7623", "C7624", "C7625", "C7627", "C7628", "C7629", "C7630", "C7631", "C7632", "C7633", "C7634", "C7635", "C7636", "C7637", "C7638", "C7639", "C7640", "C7641", "C7642", "C7643", "C7644", "C7645", "C7646", "C7647", "C7648", "C7649", "C7650", "C7651", "C7652", "C7653", "C7654", "C7655", "C7656", "C7657", "C7658", "C7659", "C7660", "C7661", "C7662", "C7700", "C7701", "C7702", "C7703", "C7704"]
6+
helpviewer_keywords: ["C7500", "C7501", "C7502", "C7503", "C7504", "C7505", "C7506", "C7507", "C7508", "C7509", "C7511", "C7512", "C7513", "C7514", "C7515", "C7516", "C7517", "C7518", "C7519", "C7520", "C7521", "C7522", "C7523", "C7524", "C7525", "C7526", "C7527", "C7528", "C7529", "C7530", "C7531", "C7532", "C7533", "C7534", "C7535", "C7536", "C7537", "C7538", "C7539", "C7540", "C7541", "C7542", "C7543", "C7544", "C7545", "C7546", "C7547", "C7548", "C7549", "C7550", "C7551", "C7552", "C7553", "C7554", "C7555", "C7556", "C7557", "C7558", "C7559", "C7560", "C7561", "C7562", "C7563", "C7564", "C7565", "C7566", "C7567", "C7568", "C7569", "C7570", "C7571", "C7572", "C7573", "C7574", "C7575", "C7576", "C7577", "C7578", "C7579", "C7580", "C7581", "C7582", "C7583", "C7584", "C7585", "C7586", "C7587", "C7588", "C7589", "C7590", "C7591", "C7592", "C7593", "C7594", "C7595", "C7596", "C7597", "C7599", "C7600", "C7601", "C7602", "C7603", "C7604", "C7605", "C7606", "C7607", "C7608", "C7609", "C7610", "C7611", "C7612", "C7613", "C7614", "C7615", "C7616", "C7617", "C7618", "C7619", "C7620", "C7621", "C7622", "C7623", "C7624", "C7625", "C7627", "C7628", "C7629", "C7630", "C7631", "C7632", "C7633", "C7634", "C7635", "C7636", "C7637", "C7638", "C7639", "C7640", "C7641", "C7642", "C7643", "C7644", "C7645", "C7646", "C7647", "C7648", "C7649", "C7650", "C7651", "C7652", "C7653", "C7654", "C7655", "C7656", "C7657", "C7658", "C7659", "C7660", "C7661", "C7662", "C7700", "C7701", "C7702", "C7703", "C7704"]
77
---
88
# Compiler errors C7500 through C7999
99

@@ -25,7 +25,7 @@ The articles in this section of the documentation explain a subset of the error
2525
| Compiler error C7507 | '%$S': the declared type of a variable concept shall be 'bool' |
2626
| Compiler error C7508 | unrecognized partition name '%s' in metadata for module '%s' |
2727
| Compiler error C7509 | '%s': malformed module metadata. |
28-
| Compiler error C7510 | '%$I': use of dependent template/typename name must be prefixed with 'template/typename' |
28+
| [Compiler error C7510](compiler-error-c7510.md) | '*type-name*': use of dependent template/type name must be prefixed with 'template/typename' |
2929
| Compiler error C7511 | '%$I': 'typename' keyword must be followed by a qualified name |
3030
| Compiler error C7512 | '%$L': is not a valid operator for a fold-expression |
3131
| Compiler error C7513 | '%$I': cannot deduce the type of the placeholder |

docs/error-messages/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3114,6 +3114,8 @@
31143114
items:
31153115
- name: Compiler errors C7500 through C7999
31163116
href: compiler-errors-2/compiler-errors-c7500-through-c7999.md
3117+
- name: Compiler error C7510
3118+
href: compiler-errors-2/compiler-error-c7510.md
31173119
- name: Compiler error C7626
31183120
href: compiler-warnings/c5208.md
31193121
- name: Compiler warnings C4000 Through C5999

docs/overview/cpp-conformance-improvements-2017.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1785,7 +1785,7 @@ int main()
17851785
17861786
In [`/permissive-`](../build/reference/permissive-standards-conformance.md) mode, the compiler now requires the **`template`** keyword to precede a template-name when it comes after a dependent nested-name-specifier.
17871787
1788-
The following code in [`/permissive-`](../build/reference/permissive-standards-conformance.md) mode now raises C7510:
1788+
The following code in [`/permissive-`](../build/reference/permissive-standards-conformance.md) mode now raises [C7510](../error-messages/compiler-errors-2/compiler-error-c7510.md):
17891789
17901790
```cpp
17911791
template<typename T> struct Base

docs/overview/cpp-conformance-improvements.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,16 +274,21 @@ void example()
274274

275275
### Function template bodies containing `if constexpr` statements
276276

277-
Template function bodies containing **`if constexpr`** statements have some [`/permissive-`](../build/reference/permissive-standards-conformance.md) parsing-related checks enabled. For example, in Visual Studio 2017 the following code produces C7510 only if the **`/permissive-`** option isn't set. In Visual Studio 2019 the same code raises errors even when the **`/permissive-`** option is set:
277+
In Visual Studio 2019 under **`/std:c++latest`**, template function bodies that have **`if constexpr`** statements have extra parsing-related checks enabled. For example, in Visual Studio 2017 the following code produces [C7510](../error-messages/compiler-errors-2/compiler-error-c7510.md) only if the **`/permissive-`** option is set. In Visual Studio 2019 the same code raises errors even when the **`/permissive`** option is set:
278278

279279
```cpp
280-
template <typename T>
280+
// C7510.cpp
281+
// compile using: cl /EHsc /W4 /permissive /std:c++latest C7510.cpp
282+
#include <iostream>
281283

284+
template <typename T>
282285
int f()
283286
{
284287
T::Type a; // error C7510: 'Type': use of dependent type name must be prefixed with 'typename'
288+
// To fix the error, add the 'typename' keyword. Use this declaration instead:
289+
// typename T::Type a;
285290

286-
if constexpr (T::val)
291+
if constexpr (a.val)
287292
{
288293
return 1;
289294
}
@@ -301,7 +306,7 @@ struct X
301306

302307
int main()
303308
{
304-
return f<X>();
309+
std::cout << f<X>() << "\n";
305310
}
306311
```
307312

0 commit comments

Comments
 (0)